[haXe] Dynamically called methods in haXe

Michael Pliskin pl at tepkom.ru
Tue Apr 10 09:00:06 CEST 2007


Hello Aaron,

  I am not sure of the proper name, I think 'reflection' is a generic
  word for this, but what you need is to use Reflect class. To call a
  method, first get a reference to it:
    var method = Reflect.field(_root, "foo");
  then call it:
    var result = Reflect.callMethod(_root, method, []);
  the last argument here is an array of parameters you want to pass.

  You might also want to take a look in Reflect API docs:
  http://haxe.org/api/Reflect

  You can use the form like _root["foo"], but it has to be called
  using untyped keyword like
    untyped _root["foo"]
  and will only work for flash platform. It is also untyped, so this
  way is strongly discouraged in haXe.

AF> Probably a simple answer to this one, I did some searching but sometimes
AF> I'm not sure what the proper 'term' is for whatever I'm searching for.

AF> Anyhow, I need to replicate this scenario in haXe, here is an example of
AF> a dynamic method call that would work in flash or javascript:

AF> //in flash
AF> var myMethod = "foo";
AF> _root[myMethod]();

AF> function foo(){
AF>     trace("bar");
AF> }

AF> //same kinda thing in js
AF> var myMethod = "foo";
AF> window[myMethod]();

AF> function foo(){
AF>     alert("bar");
AF> }

AF> Any idea how to accomplish this kind of thing in haXe? When I try a 
AF> similar syntax, the compiler tells me 'myMethod' should be an Int...and
AF> I can't think of another way that might work.  Also, is there a proper
AF> 'name' or 'term' for this type of operation?

AF> Thanks,
AF> Aaron






-- 
Best regards,
 Michael                            mailto:pl at tepkom.ru




More information about the Haxe mailing list