@:autoBuild Best way to access all fields from the whole inheritance chain

89 views
Skip to first unread message

Francis Bourre

unread,
Mar 6, 2016, 11:12:27 AM3/6/16
to Haxe
Hi guys,

I noticed that @:autobuild only give access to the fields of the current class.

What is the best way to get all the available fields from the inheritance chain with @:autoBuild ?

Thanks in advance for your answer.

Nicolas Cannasse

unread,
Mar 6, 2016, 11:32:01 AM3/6/16
to haxe...@googlegroups.com
Le 06/03/2016 17:12, Francis Bourre a écrit :
> Hi guys,
>
> I noticed that @:autobuild only give access to the fields of the current
> class.
>
> What is the best way to get all the available fields from the
> inheritance chain with @:autoBuild ?

When the parent class is built you can store the fields (or just the
information you need) into some metadata, and retrieve it later.

Best,
Nicolas

Francis Bourre

unread,
Mar 6, 2016, 1:27:44 PM3/6/16
to Haxe
Thank you Nicolas for your feedback.

In my case @:autobuild is only triggered on the extended class because this class implements the interface below (and the super class doesn't):
@:autoBuild( hex.core.AnnotationReader.readMetadata() )interface IAnnotationContainer 

I tried this way to retrieve the information of the super class from the macro:
var superClass = Context.getLocalClass().get().superClass ;
var superFields = superClass.t.get().fields.get();

But the result is typed 'Array<ClassField> and not Array<Field>, so I can't reuse my previous parsing/buidling algorithm on it, to get informations recursively.

I discovered macro recently and I'm stuck here. Any idea that could help me to parse the whole fields inheritance chain ?

Thanks again.

Nicolas Cannasse

unread,
Mar 6, 2016, 1:51:27 PM3/6/16
to haxe...@googlegroups.com
Le 06/03/2016 19:27, Francis Bourre a écrit :
> Thank you Nicolas for your feedback.
>
> In my case @:autobuild is only triggered on the extended class because
> this class implements the interface below (and the super class doesn't):
> /@:autoBuild( hex.core.AnnotationReader.readMetadata() )interface
> IAnnotationContainer /
> /
> /
> I tried this way to retrieve the information of the super class from the
> macro:
> /var superClass = Context.getLocalClass().get().superClass ;/
> /var superFields = superClass.t.get().fields.get();/
>
> But the result is typed 'Array<ClassField> and not Array<Field>, so I
> can't reuse my previous parsing/buidling algorithm on it, to get
> informations recursively.

Yes, you get typed AST, not "parsed" one, which is lost when typing the
class.

The idea is that any data that you want to be passed by your superclass
to its subclasses have to be stored into metadata. Metadata can store
"parsed" expressions.

Best,
Nicolas



Simon Krajewski

unread,
Mar 6, 2016, 2:05:00 PM3/6/16
to haxe...@googlegroups.com
Hi,

the current Haxe development version has a new feature regarding that:
If a field is annotated with @:astSource metadata, the compiler will
store the field expression as an argument to that metadata so it
basically becomes @:astSource(fieldExpression). Your macro can then
inspect that metadata and extract the expression.

Simon

Francis Bourre

unread,
Mar 7, 2016, 4:50:52 AM3/7/16
to Haxe
Thank you guys for your lights. Last question, is there a way to parse all the field classes with the same macro without adding @:autoBuild everywhere ?

Nicolas Cannasse

unread,
Mar 7, 2016, 8:00:18 AM3/7/16
to haxe...@googlegroups.com
autoBuild in inherited, so every extends (or implements) will propagate
it to subclasses.

Best,
Nicolas

Francis Bourre

unread,
Mar 8, 2016, 2:58:43 AM3/8/16
to Haxe
Thanks for your help.
Reply all
Reply to author
Forward
0 new messages