I was wondering if there is a way to initialize the array to a given size n(as in the C ++ constructor std::vectoror @steve richey below, without breaking the hint element)?
My thought was to drop the ndefault elements into the newly created array, but even this seems impossible, because there seems to be no way to define the default element, as shown in the original question below:
- original question -
I am trying to write a static function in haxe (starting with 3.0.1) to create an array of a given size n containing types of arbitrary type T. I could not figure out how to use Type.createEmptyInstance () correctly for this. The closest I can get is:
class Main {
static public function new_array<T>(n:Int):Array<T> {
var a:Array<T> = new Array<T>();
var t:T = new T();
for (i in 0...n)
a.push(Type.createEmptyInstance(Type.getClass(t)));
return a;
}
static public function main() {
var a:Array<Int> = new_array(3);
}
}
new_array() . : Main.hx:4: characters 12-19 : Only generic type parameters can be constructed.
, T.
,