Construction of generic type parameters

I have a generic typed class that is supposed to make an instance of the type that was specified

I read through the related docs:

I’m confused why I can’t do the folllowing:


import haxe.Constraints.Constructible;
import openfl.display.DisplayObject;
import openfl.display.Sprite;

class Main extends Sprite {
	public function new () {
		super ();
		var test = new GenTest<Sprite>();
		var sprite = test.makeSprite();
		trace (sprite);
		// [object Sprite]
		sprite = test.makeT();
		trace (sprite);

class GenTest<T:DisplayObject> {
	public function new() {} 
	public function makeSprite():Sprite { return make(); }
	public function makeT():T { return make(); }
	function make<K:Constructible<Void->Void>>():K { return new K(); } 

if GenTest is @:generic i get:

Error:(59, 9) Constraint check failure for make.K
Error:(59, 9) GenTest.T should be haxe.Constructible<Void -> Void>

if it is not generic, i get:

Error:(45, 14) Constraint check failure for make.K
Error:(45, 14) GenTest.T should be haxe.Constructible<Void -> Void>
Error:(56, 57) Only generic type parameters can be constructed
Error:(45, 14) While specializing this call

For @:generic version you need to add haxe.Constraints.Constructible constraint to T (see its doc for more info).

Non-@:generic version cannot do new T because type parameters are erased at run-time, so you have to either pass the class value for use with Type.createInstance or a factory function (e.g., see - Haxe - The Cross-platform Toolkit).