How do I use 'Constructible'?

Hi how do I use the constructible feature? Googling it just links me to an extremely sparse haxe documentation. Are there any good examples on the usage?

I’m trying to make a function that only accepts the class definition, not the instance:

class Test {
	static function main() {
		testFunc(TestClass);       // This should work
		testFunc(new TestClass()); // This should error
	}

	@:generic
	static function testFunc<T>(cls:T) {}
}

class TestClass {
	public function new() {}
}

How would I do this?

import haxe.Constraints;

class Test {
	static function main() {
		testFunc(TestClass);       // This should work
		testFunc(new TestClass()); // This should error
	}

	@:generic
	static function testFunc<T:Constructible<Void->Void>>(cls:Class<T>):T {
		return new T();
  }
}

class TestClass {
	public function new() {}
}
2 Likes

not using Constructible, just an alternative:

class Test {
	static function main() {
		testFunc(TestClass.new);
	}

	static function testFunc<T>(factory:Void->T):T {
		return factory();
	}
}

class TestClass {
	public function new() {}
}
1 Like