Skip to content

Instantly share code, notes, and snippets.

@nadako
Created April 29, 2013 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadako/5484498 to your computer and use it in GitHub Desktop.
Save nadako/5484498 to your computer and use it in GitHub Desktop.
Why, neko?
-cp .
-main Main
-x Main.n
class Main
{
private static function main()
{
function newEmptyHandler():Void->Void
{
return function():Void {};
}
var a:Void->Void = newEmptyHandler();
var b:Void->Void = newEmptyHandler();
trace(a);
trace(b);
trace(a == b);
trace(Reflect.compareMethods(a, b));
}
}
Main.hx:12: #function:0
Main.hx:13: #function:0
Main.hx:14: true
Main.hx:15: true
@nadako
Copy link
Author

nadako commented Apr 29, 2013

What's even more confusing, this works as expected:

class Main
{
private static function main()
{
var a:Void->Void = function():Void {};
var b:Void->Void = function():Void {};
trace(a);
trace(b);
trace(a == b);
trace(Reflect.compareMethods(a, b));
}
}

Output:

Main.hx:7: #function:0
Main.hx:8: #function:0
Main.hx:9: false
Main.hx:10: false

@nadako
Copy link
Author

nadako commented Apr 29, 2013

After some research, I found out that it depends on whether function holds any context from outer scope:

class Main
{
private static function newHandlerWithContext()
{
var ctx = "something";
return function() { ctx; };
}

private static function newHandlerWithoutContext()
{
    return function() { };
}

private static function main()
{
    var a = newHandlerWithContext();
    var b = newHandlerWithContext();
    trace(a == b);
    trace(Reflect.compareMethods(a, b));

    var a = newHandlerWithoutContext();
    var b = newHandlerWithoutContext();
    trace(a == b);
    trace(Reflect.compareMethods(a, b));
}

}

Output:

Main.hx:18: false
Main.hx:19: false
Main.hx:23: true
Main.hx:24: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment