haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Array expression reification for function arguments

Open Pauan opened this issue 9 years ago • 4 comments

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 )

Pauan avatar Sep 17 '16 09:09 Pauan

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.

Simn avatar Sep 17 '16 09:09 Simn

Maybe we can make it work if you actually pass the correct type

That would be really useful!

nadako avatar Sep 17 '16 09:09 nadako

@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 )

Pauan avatar Sep 17 '16 09:09 Pauan

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

Geokureli avatar Oct 03 '24 17:10 Geokureli