你见过Luca的固定点例子Haxe 3和摘要吗? 它在这里: https://groups.google.com/forum/?fromgroups=#!topic/haxelang/JsiWvl-c0v4
总结起来,与新HAXE 3抽象类型,你可以定义将被编译成一个int类型:
abstract Fixed16(Int)
{
inline function new(x:Int) this = x;
}
您还可以定义“转换函数” ,这将让你一个浮动自动转换成Fixed16:
@:from public static inline function fromf(x:Float) {
#if debug
if (x >= 32768.0 || x < -32768.0) throw "Conversion to Fixed16 will overflow";
#end
return new Fixed16(Std.int(x*65536.0));
}
这里的秘密是@:from
元数据。有了这个代码,你就已经可以宣告固定的类型是这样的:在这里
@:op(A+B) public inline static function add(f:Fixed16, g:Fixed16) {
#if debug
var fr:Float = f.raw();
var gr:Float = g.raw();
if (fr+gr >= 2147483648.0 || fr+gr < -2147483648.0) throw "Addition of Fixed16 values will overflow";
#end
return new Fixed16(f.raw()+g.raw());
}
再次,秘密:
var x:Fixed16 = 1.2;
卢卡的已定义的一些运营商,使他们的工作更容易,像在@:op(A + B)元数据中,它将注释在处理添加时可能会调用此函数。完整的GIST代码可在https://gist.github.com/deltaluca/5413225获得,您可以通过http://haxe.org/manual/abstracts
了解更多关于摘要的信息!太美了! – MetaChrome 2013-05-11 12:26:40