haxe
haxe copied to clipboard
Array expression reification for function arguments
With haxe version 3.2.1, I tried writing the following macro:
import haxe.macro.Expr;
class Test {
static macro function fn(args: Array<Expr>) {
return macro function ($a{args}) {};
}
}
However, this failed with the following compile errors:
Test.hx:5: characters 29-30 : Unexpected {
Test.hx:5: characters 30-34 : Missing ;
Test.hx:5: characters 35-36 : Unexpected )
That shouldn't work with Array<Expr> because that's not what the data structure expects there. Maybe we can make it work if you actually pass the correct type (http://api.haxe.org/haxe/macro/FunctionArg.html) though.
Maybe we can make it work if you actually pass the correct type
That would be really useful!
@Simn Ah yes, you're right. In that case, it would be nice if the following code worked:
import haxe.macro.Expr;
class Test {
static macro function fn(exprs: Array<Expr>) {
var args = exprs.map(function (x) {
return switch (x.expr) {
case EConst(CIdent(x)): x;
default: throw 'Unexpected argument $x';
};
});
return macro function ($a{args}) {};
}
}
It currently gives the same errors as above:
Test.hx:12: characters 29-30 : Unexpected {
Test.hx:12: characters 30-34 : Missing ;
Test.hx:12: characters 35-36 : Unexpected )
Wanted to chime in to say I would also like to be able to define function headers via array reification: https://community.haxe.org/t/define-function-args-via-reification/4263