In Haxe how do you implement array operators for a class?
I am trying to write a class in Haxe supporting array like access using the []
operator such as:
var vec开发者_运维问答tor = new Vec3();
trace(vector.length); // displays 3
vector[0] = 1; // array like access to the class, how?
vector[1] = 5.6; // more array access
vector[2] = Math.PI; // yet more array access
The problem is I don't know how to define a class such that it allows the []
operator. I need this class, rather than using an Array<Float>
or List<Float>
because there is some trickery going on with it to support my animation system which references to parts of vectors using storyboards (see http://www.youtube.com/watch?v=ijF50rRbRZI)
In C# i could write:
public float this[index] { get { ... } set { .... } }
I've read the Haxe documentation and found ArrayAccess<T>
, but the interface is empty. That is I don't understand how to implement it, or if I just implement ArrayAccess<Float>
... what method on my class would be called to retrieve Float
at said index?
Haxe doesn't support operators overload (yet) so you will have to use a get/set pair. You can use inline if the magic that happens inside your methods need to be optimized for speed.
精彩评论