是否可以使用 Haxe 编写使用类型参数的 AS3 库?
首先是一些背景知识:我正在寻找一种方法来创建一个“集合”库,该库将基于 Flash Player 版本的实现(FP10 上的矢量,FP9 上的数组)从调用代码中抽象出来。我已经编写了一个小型 AS3 库来做到这一点,但是......
- 性能很差(特别是因为两级间接和对数组实现的运行时类型检查)
- ......代码很丑陋(因为 Vector类型需要在编译时定义 我需要一个基于仅包含受支持类型的枚举返回具体 Vector 实例的工厂)
我目前正在研究 Haxe 作为一个可能的解决方案,因为它支持类型参数并且能够编译为各种 Flash播放器版本(显然编译成更优化的字节码)。
现在,我的问题是:有没有一种方法可以在 Haxe 中编写一个库,可以在 AS3 代码中使用
var foo:IMyInterface = new MyImplementation(int);
var bar:IMyInterface = new MyImplementation(getDefinitionByName("my.package.MyClass"));
IMyInterface
公开所需的方法(push
、>流行,...)?
基本想法是,我想在运行时提供类型信息,并获得一个类型安全的 Flash Player 版本独立“集合”,以便在调用代码中使用,而不必担心各处的条件编译片段。
Haxe 可以做类似的事情吗?如果可以,我该如何让它发挥作用?
First a little background: I'm looking for a way to create a "collection" library that abstracts the Flash Player version based implementation (Vector on FP10, Array on FP9) away from the calling code. I've already written a small AS3 lib doing that but...
- ...the performance is bad (especially because of two levels of indirection and the runtime type checks on the Array implementation)
- ...the code is ugly (since Vector types need to be defined at compiletime I needed a factory returning concrete Vector instances based on an Enum that contains only the supported types)
I'm currently looking into Haxe as a possible solution since it supports type parameters and is able to compile to various Flash Player versions (and apparently compiles into mmore optimized bytecode).
Now, my question is: Is there a way to write a library in Haxe that can be used like this in AS3 code
var foo:IMyInterface = new MyImplementation(int);
var bar:IMyInterface = new MyImplementation(getDefinitionByName("my.package.MyClass"));
with IMyInterface
exposing the required methods (push
, pop
, ...)?
The basic idea is that I want to provide the type information at runtime and get a typesafe Flash Player version independent "collection" for use in the calling code without having to bother with conditional compilation fragments all over the place.
Can Haxe do something like that and if yes, how can I make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Haxe 中有机会覆盖 Haxe 中的本机类(例如
int
)。查看元数据手册。 2.06版本中添加了元数据。至于
getDefinitionByName()
方法的类似物。看一下Type
resolveClass() 方法> 类。There's an opportunity in Haxe to override native classes (e.g.
int
) in Haxe. take a look at the Metadata manual. Metadata has been added in version 2.06.As for analogue of
getDefinitionByName()
method. Take a look atresolveClass()
method of theType
class.