Setting Vector content

I am converting some AS3 classes, and I stumbled on a problem: how do I assign a predefined list of values to a Vector (openfl.Vector)?
In AS3 it worked like this

var cylinder:Vector.<Number> = Vector.<Number>([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);

but in Haxe I can’t do like this

var cylinder:Vector<Float> = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];

as it recognizes the […] as an Array of Ints.

var cylinder:Vector<Float> = cast([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1], Vector<Float>)

a acceptable/feasable/correct solution? It doesn’t look cool.

I don’t want to do a loop to insert each element by .set method indeed.

Try var cylinder:Vector<Float> = Vector.ofArray([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1.0]);

Really?! 1.0? Ooook.
Thank you Joshua :wink:

You might be able to use cast, but the tricky thing about this API is the right-hand statement does not know what type you are using in the left-hand statement. As a result, the right-hand is based on the type of the incoming array. If you specify all integers, then it will become a Vector<Int>, and won’t match up when it tries to assign the value to the left-hand side. I’m open to ideas on this.

If you don’t like the 1.0, there are a few other ways to do the same thing:

var cylinder:Vector<Float> = Vector.ofArray([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,(1:Float)]);

var cylinderArray:Array<Float> = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1];
var cylinder:Vector<Float> = Vector.ofArray(cylinderArray);

var cylinder:Vector<Float> = Vector.ofArray(([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]:Array<Float>));

I know the problem, the right-hand side type is recognized apart, and your trick is a good suggestion for the compiler, but it is a workaround and sometimes I feel too perfectionist to even think about it :yum:

The third/last one looks the more clean to me: I didn’t know I could assign the type that way, on the fly in the value itself.