I am trying this:
var cls_arr:Array<Class > ;
cls_arr = [MovieClip, SimpleButton, TextField];
trace(cls_arr[0]);)
trace(cls_arr[1]);)
trace(cls_arr[2]);)
and get error:
myapp/Main.hx:54: characters 19-24 : Invalid number of type parameters for Class
In order to use mixed types, you can use Array<Dynamic>
.
From the Haxe documentation:
Haxe does not allow arrays of mixed types unless the parameter type is forced to Dynamic
:
class Main {
static public function main() {
// Compile Error: Arrays of mixed types are only allowed if the type is
// forced to Array<Dynamic>
// var myArray = [10, "Bob", false];
// Array<Dynamic> with mixed types
var myExplicitArray:Array<Dynamic> = [10, "Sally", true];
}
}
Cannot I use something like Array<Class<T>>
? I tried it, but it says T is not recognized.
It appears so!
Looks like in your case you can use something like Array<Class<DisplayObject>>
.
Or more generally, Array<Class<Dynamic>>
.
Yes Array<Class<Dynamic>> or Array<Class<T>> seems to work
But there is a problem when I try to use a class as variable with “cast”.
var mc_class:Dynamic = MovieClip;
var mc2 = cast(instnace_mc, mc_class);
OR
var mc_class:Class<T>= MovieClip;
var mc2 = cast(instnace_mc, mc_class);
both of them don’t work. The cast function is not allowing variable type. Is their any way out?
In order to create an instance to a dynamic class like that, you can use the Type.createInstance helper.
var cls_arr:Array<Class<Dynamic>>;
cls_arr = [MovieClip, SimpleButton, TextField];
var cls:Class<Dynamic> = cls_arr[0];
var mc = Type.createInstance(cls, []);
A little strange, but should get the job done!