1

Is there a simple way to prevent $bind generation when passing around class member functions?

I have an object that takes in a function which will be called in an arbitrary interval. The object always binds itself as the 'this' binding before calling the function (this is done in the native side), therefore the call to $bind itself is unnecessary. However, I can't seem to find a simple way to prevent $bind from being emitted any time I grab a member function by value.

The only way I've found is to use __js__ with a string literal of the member function name, which I would rather avoid... Is there a typed way to do so? Or something a bit nicer? A way to still use haxe syntax w/ identifiers instead of a string literal?

Example:

private function onSpawn():Void
{
    this.setAct( act ); // Will generate JS: this.setAct($bind(this,this.act));
                        // Id like to simply have it generate: this.setAct( this.act );

    // Mitigated like this:
    this.setAct( untyped __js__("this.act") );
}

private function act( dt:Float ):Void
{
    ...
}

Thank you.

Harold B.
  • 65
  • 4
  • Not sure if there is any method except modifying the generator (ocaml). What is the exactly problem you are experiencing with $bind? – KevinResoL Mar 22 '17 at 15:13
  • $bind adds another unnecessary function call where it isn't needed. I'm using javascript interpreter and these functions are called every frame for many objects, which means an unnecessary performance hit. (See https://github.com/svaarala/duktape/issues/1300#issuecomment-273003473). Not only that but $bind adds 3 property lookups as well each time it's called, which is also slow: **f.method.apply(f.scope, .. )** – Harold B. Mar 22 '17 at 15:33

1 Answers1

2

You use macro to mask out the untyped expression, but this is quite dangerous. Any reference to "this" will fail.

http://try-haxe.mrcdk.com/#70ee4

Btw, I think the compiler may be optimized to not generate $bind if the function code doesn't involve "this". You may want to raise an issue in the github repo about that.

KevinResoL
  • 982
  • 8
  • 19
  • This solution is enough for my particular situation. Certainly much cleaner than my current workaround. Forgot there were other forms of macros than build macros. :) As for your warning re 'this' binding, understood -- In my case the 'act' is being called from the native side which is setting the 'this' binding to the object itself already. Thank you! – Harold B. Mar 22 '17 at 16:20