i'm trying instantiate class based on function type parameter.
although documentation says possible, can't make work.
consider following code:
// dialog base class // every dialog in application derive class dialog { public function new() { // stuff here } } // 1 of possible dialogs in application // extends dialog class testdialog extends dialog { public function new() { super(); // more stuff } } // simple class tries instantiate specialized dialog, testdialog class someappclass { public function new() { var instance = create(testdialog); } @:generic function create<t:dialog>(type:class<t>):t { return new t(); } }
this doesn't work following error:
create.t not have constructor
clearly, i'm doing wrong, what?
specialdialog
have different constructor dialog
. have constraint it , constraint dialog
.
package; typedef constructible = { public function new():void; } // dialog base class // every dialog in application derive class dialog { public function new() { trace("dialog"); } } class superdialog extends dialog { public function new() { super(); trace("super dialog"); } } // simple class tries instantiate specialized dialog, testdialog class someappclass { public function new() { var dialog = create(dialog); var superdialog = create(superdialog); } @:generic public static function create<t:(constructible,dialog)>(type:class<t>):t { return new t(); } } class test { static public function main() { new someappclass(); } }
Comments
Post a Comment