Class has a type parameter , so if you are going to take a class as an argument, you need to specify a type parameter.
Accept any class:
function foo(myClassRef:Class<Dynamic>):Void { var myVar = Type.createInstance( myClassRef, [constructorArg1, constructorArg2....] ); trace( Type.typeof(myVar) ); }
Accept only sys.db.Object class or subclasses:
function foo(myClassRef:Class<sys.db.Object>):Void { var myVar = Type.createInstance( myClassRef, [] ); trace( Type.typeof(myVar) ); }
Haxe 3 also allows common features :
@:generic function foo<T:Dynamic>(t:Class<T>) { var myVar = new T(); trace( Type.typeof(myVar) ); }
Here you declare the function common, which means that for each parameter of a different type, a different version of the function will be compiled. You accept a Class, where T is a type parameter - in this case - dynamic, so it will work with any class. Finally, using common functions, you can write new T() , which may seem like a more natural syntax, and on some platforms there may be performance benefits.
Jason o'neil
source share