在 Haxe 中如何为类实现数组运算符?
我正在尝试在 Haxe 中编写一个类,支持使用 []
运算符进行类似数组的访问,例如:
var vector = 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
问题是我不知道如何定义一个类,以便它允许 [ ]
运算符。我需要这个类,而不是使用 Array
或 List
因为它存在一些技巧来支持我的动画系统(引用部件)使用故事板的向量(请参阅 http://www.youtube.com/watch?v=ijF50rRbRZI)
在 C# 中我可以这样写:
public float this[index] { get { ... } set { .... } }
我已阅读 Haxe 文档并找到了 ArrayAccess
,但界面是空的。也就是说,我不明白如何实现它,或者如果我只是实现ArrayAccess
...我的类上的什么方法将被调用来检索在所述索引处浮动
?
I am trying to write a class in Haxe supporting array like access using the []
operator such as:
var vector = 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Haxe 还不支持运算符重载,因此您必须使用 get/set 对。如果方法内部发生的魔法需要优化速度,则可以使用内联。
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.