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:
package;
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);
}
}
@:generic
class GenTest<T:DisplayObject> {
public function new() {}
public function makeSprite():Sprite { return make(); }
public function makeT():T { return make(); }
@:generic
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