Possible error with function binding and getters in Haxe?

Just ran into this problem in Haxe and wondered if it was a mistake or if it was done on purpose ...

I bound a function that prints a timestamp. The timestamp in this case was a getter in my globals class. I expected that if I had to wait a few seconds and then call the associated function, it would use the getter value at the time the function was bound. That was not so. Instead, it seems to call a getter to get the current value each time.

I checked if this happened if I switched from using getter to a regular function call to get my timestamp as my parameter. The latter works as expected.

function printTime(time:Int):Void {
    trace("The time is: " + time);
}

var p:Void->Void = printTime.bind(Globals.timestampgetter); 
var p2:Void->Void = printTime.bind(Global.timestampfunc());
// wait 5 seconds
p(); // prints CURRENT timestamp, i.e. adds the 5 seconds that passed
p2(); // prints time at which printTime.bind was called

EDIT: Forgot to mention ... I am using Haxe 3.1.3 and OpenFL 3.0.0 beta, compiling for the purpose of Flash.

+4
source share
3 answers

After several attempts, I reduced the test case to the next, and I can confirm that this is a bug in the Flash generator. I reported this here: https://github.com/HaxeFoundation/haxe/issues/4089

class Test {
    static function main() {
        function printTime(time:Float)
            trace("The time is: " + time);
        timestamp = timestampfunc();
        var t = timestampfunc();
        var p1 = printTime.bind(timestamp); 
        var p2 = printTime.bind(t); 
        var p3 = printTime.bind(timestampfunc());
        p1();
        p2();
        p3();

        haxe.Timer.delay(function() {
            t = timestamp = timestampfunc();

            p1();
            p2();
            p3();
        }, 1000);
    }
    public static var timestamp : Float;
    static function timestampfunc() return Date.now().getTime();
}
+3
source

I tried my code and it works as expected for me. The values ​​are set in bindtime and do not change, even if you delay calls pand p2.

Here is the code I tested:

class Test {
    static function main() {
        function printTime(time:Float):Void {
            trace("The time is: " + time);
        }

        var p = printTime.bind(Test.timestampgetter); 
        var p2 = printTime.bind(Test.timestampfunc());
        p();
        p2();

        haxe.Timer.delay(function() {
            p();
            p2();
        }, 1000);
    }

    public static var timestampgetter(get, null) : Float;

    static function timestampfunc() return Date.now().getTime();
    static function get_timestampgetter() return Date.now().getTime();
}

You can test it here: http://try.haxe.org/#C85Ce

+2
source

... , , "default" "get" getter.

- . :

class Test {
    static function main() {
        function printTime(time:Float):Void {
            trace("The time is: " + time);
        }

        updateTimestamp();
        var p = printTime.bind(Test.timestampgetter); 
        var p2 = printTime.bind(Test.timestampfunc());
        p();
        p2();

        haxe.Timer.delay(function() {
            p();
            p2();
        }, 1000);
    }

    static function updateTimestamp():Void {
        timestampgetter = Date.now().getTime();
        haxe.Timer.delay(updateTimestamp, 1000);
    }

    public static var timestampgetter(default, null) : Float;

    static function timestampfunc() return Date.now().getTime();
    static function get_timestampgetter() return Date.now().getTime();
}
+2

All Articles