Haxe中的“物理上相等”是什么意思?

青少年

我一直在玩Neko Modules,但我认为我得到了一些不一致的行为。

var funcs = 0;
var objs = 0;
for (i in 0...m.globalsCount())
{
    var obj:Dynamic = m.getGlobal(i);

    if (Reflect.compareMethods(obj, init))
        trace("matched");

    if (Reflect.isFunction(obj))
        funcs++;
    else if (Reflect.isObject(obj))
        objs++;
}
trace('Functions: $funcs');
trace('Objects: $objs');

在上面的代码中,当我第一次运行它时,我总共获得了4487个函数。如果我删除一个函数,重新生成并运行,则会得到预期的4486。

我加入了compareMethods比较,比较objinit,这里init是我的主文件中声明的功能,但跟踪是从来没有输出。

我浏览了该compareMethods函数的代码提示,然后偶然发现了以下术语:if 'f1' and the 'f2' are **physically** equal

现在,它们都是功能,在Haxe手册中没有任何地方提及物理功能。所以我确实有一个两部分的问题。

什么是物理功能,如何如您期望的那样获得跟踪结果?先感谢您。

米哈伊尔·伊格纳季耶夫(Mikhail Ignatiev)

根据haxe单元测试(以及Reflect的js源),仅当您将同一对象的任何方法与其自身进行比较时,才Reflect.compareMethods返回true

// https://github.com/HaxeFoundation/haxe/blob/ff3d7fe6911ab84c370b1334d537a768a55cca56/tests/unit/src/unit/TestReflect.hx
// 
// t(expr) - expr should be true 
// f(expr) - expr should be false 

function testCompareMethods() {
    var a = new MyClass(0);
    var b = new MyClass(1);
    t( Reflect.compareMethods(a.add,a.add) );
    f( Reflect.compareMethods(a.add,b.add) );
    f( Reflect.compareMethods(a.add,a.get) );
    f( Reflect.compareMethods(a.add,null) );
    f( Reflect.compareMethods(null, a.add) );
    /*
        Comparison between a method and a closure :
        Not widely supported atm to justify officiel support
        var fadd : Dynamic = Reflect.field(a, "add");
        var fget : Dynamic = Reflect.field(a, "get");
        t( Reflect.compareMethods(fadd, fadd) );
        t( Reflect.compareMethods(a.add, fadd) );
        t( Reflect.compareMethods(fadd, a.add) );
        f( Reflect.compareMethods(fadd, fget) );
        f( Reflect.compareMethods(fadd, a.get) );
        f( Reflect.compareMethods(fadd, null) );
    */
}

另外,可能的用例

class Test {
    static function main() {
        var a = new A();
        var i:I = a;
        trace(Reflect.compareMethods(a.test, i.test)); //returns true
    }
}

interface I
{
    function test():Void;
}

class A implements I
{
    public function new() {}
    public function test() {}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章