Function Pointer, missing scope when called.

58 views
Skip to first unread message

TroyWorks

unread,
Sep 27, 2014, 11:04:36 PM9/27/14
to haxe...@googlegroups.com

I have a class with state object/associated array. e.g.

 this.state.food = "apple";



and a couple different render methods that show it in different states like

function render_untouched():Dynamic{
return new UntouchedView(this.state);
}

function render_half_eaten():Dynamic{
return new HalfEatenView(this.state);
}




I am trying to use a function pointer for the current state.

  public var currender:Void->Dynamic;



so

currender = render_untouched;


 or
 currender = render_half_eaten;



but when i call the function pointer, it's not getting the state variable, due to some scoping issue I can't figure out.



public function render():Dynamic{
    trace(" this.state.food " + this.state.food );
  
/*   DOESN'T WORK   
* 1)
*  return Reflect.callMethod(this,Reflect.field(this,"render2"),[]);
* 2)
*   if(currender!= null){
*        res= currender();
*    }

*/
        //works
        if(currender== render2){
         res= render2();
        }else{
            res= render1();
        }
        return res;
    }


Any Ideas?  2) would be my preferred way of calling it, as 1) needing strings seems messy.

Hugh

unread,
Sep 30, 2014, 1:01:23 AM9/30/14
to haxe...@googlegroups.com
Your general approach seems reasonable-ish.  But it seems a bit odd to be caching factory functions, where you could instead be caching the resulting View.  If "new View" call does the rendering, then I guess that is Ok.  I suggest putting a trace in your render functions and see if they are getting called when you expect them to be.

The other thing is using "render==render2" could be your problem.  In haxe, you need to use "Reflect.compareMethods" to do this check.

Hugh

TroyWorks

unread,
Oct 1, 2014, 1:10:09 AM10/1/14
to haxe...@googlegroups.com
Hi Hugh

The project is actually a React.js/hx HTML5 output. so the code I gave was just an example to simplifiy.

The issue i think is that the target language is JS which doesn't support closure on the function scope like AS3 and likely other languages do, so only anonymous functions can be done via a function pointer.  In my particular case the React.js has internal props e.g. this.props.* and this.state.* which are passed in IOC style onto the view.

I find the Reflection method with strings and getField kinda messy relative to AS3, but it's working or at least can work (still need to figure out what event system to use to complete the statemachine engine).

Thanks for the ping.

Philippe Elsass

unread,
Oct 1, 2014, 4:24:32 AM10/1/14
to haxe...@googlegroups.com
I think you should provide a bit more complete example, like a mini buildable JS project where the problem can be reproduced.

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.



--
Philippe
Reply all
Reply to author
Forward
0 new messages