Trouble with macro execution order

74 views
Skip to first unread message

Drakim

unread,
Jan 27, 2016, 4:52:30 PM1/27/16
to Haxe
So I have this static array that's filled by a macro function:

static public var lookup:Array<Int> = LookupBuilder.genArray();

And all over my project, my classes call this other macro function:
LookupBuilder.addValue(42);
LookupBuilder.addValue(1337);
LookupBuilder.addValue(9000);

The idea is that the lookup array will end up looking like this:

static public var lookup:Array<Int> = [42, 1337, 9000];

However, all I get is an empty [] array instead. The reason is rather obvious, the genArray() macro ends up running before those addValue() macros run.

How do deal with this? The only macro thing I managed to get reliably running after addValue() is Context.onGenerate(), but I have no idea how I would go from it to the static lookup assignment.

Juraj Kirchheim

unread,
Jan 27, 2016, 6:00:51 PM1/27/16
to haxe...@googlegroups.com
You can use runtime metadata:

  static public var lookup:Array<Int> = haxe.rtti.Meta.getType(TheClass).lookup;

That assumes a @lookup(42, 1337, 9000) metadata is registered on TheClass in Context.onGenerate. Note that if you're using the compilation server, you have to remove old metadata before adding new one, otherwise things get weird.

Best,
Juraj


--
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.

Drakim

unread,
Jan 28, 2016, 4:02:32 AM1/28/16
to Haxe
Thanks, that solution serves my needs for now :)

Juraj Kirchheim

unread,
Jan 28, 2016, 4:31:11 AM1/28/16
to haxe...@googlegroups.com
If you need more complex data, then you can serialize it and add it through Context.addResource and then load it at runtime.

Drakim

unread,
Jan 28, 2016, 6:49:15 AM1/28/16
to Haxe
I've run into a problem, I can't manage to set the metadata in the onGenerate. haxe.macro.Compiler.addMetadata() seems to work outside but not inside the callback. Am I supposed to set it in a different way at that stage of the compilation?


On Thursday, January 28, 2016 at 12:00:51 AM UTC+1, back2dos wrote:

Juraj Kirchheim

unread,
Jan 28, 2016, 7:30:21 AM1/28/16
to haxe...@googlegroups.com
Can you provide a minimal example.

Simon Krajewski

unread,
Jan 28, 2016, 7:33:00 AM1/28/16
to haxe...@googlegroups.com
Am 28.01.2016 um 12:49 schrieb Drakim:
> I've run into a problem, I can't manage to set the metadata in the
> onGenerate. haxe.macro.Compiler.addMetadata() seems to work outside
> but not inside the callback. Am I supposed to set it in a different
> way at that stage of the compilation?

Yes, addMetadata works using type patches which means it doesn't work if
the type is already loaded. The documentation should make that clear I
guess...

You get a list of all types in the onGenerate callback, so what you have
to do is find the type you care about and use its MetaAccess object.

Simon

Drakim

unread,
Jan 28, 2016, 8:14:46 AM1/28/16
to Haxe
Yup, it worked now. Many thanks :)
Reply all
Reply to author
Forward
0 new messages