简体   繁体   中英

Haxe Macro to create function variants

I need to create a few compile-time variants of a simple function.

Is there a macro for this?

function foo_$X()
{
  //complicated stuff

  do($X);

  //more complicated stuff
}

where $X is just some constant values from a known set, say ["a","b","c"] for the sake of argument.

In this simplistic example I'd like to get the following results available at compile-time:

function foo_a()
{
 //complicated stuff

 do("a");

 //more complicated stuff
}

function foo_b()
{
 //complicated stuff

 do("b");

 //more complicated stuff
}

function foo_c()
{
 //complicated stuff

 do("c");

 //more complicated stuff
}

There is no builtin macro for that. However it's quite simple to make one.

That's the manual for building types: http://haxe.org/manual/macro-type-building.html

In your case you will probably want to mark your method with some compile-time meta, then when building the class, find the method marked with meta and add variants you compose to class fields array. The composing of variant methods itself is quite simple with the use of reification and wont need any complex AST manipulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM