-
-
Notifications
You must be signed in to change notification settings - Fork 93
Description
Recently found this:
https://github.com/lbergman/GASM/blob/develop/src/gasm/core/macros/ComponentMacros.hx#L37-L52
After some investigation, I heard you can do (macro class { ... }).fields.
As example it can be reduced to:
var fields = Context.getBuildFields();
var name = "Message";
var extraFields = (macro : {
public function name(get, null):String;
private inline function get_name() return $v{name};
public static var NAME:String = $v{name};
}).fields;
fields = fields.concat(extraFields);Some info from nadako:
One just has to remember -
macro somethingis just a syntax sugar for creating data structures (Expr, TypeDefinition, ComplexType, etc), so of course if you createmacro class {...}that's going to be a TypeDefinition, so you can access its fields normally
I almost always create fields manually, but for exprs I use reification. It doesn't feel right to create a TypeDefinition that will be thrown away later, but OTOH Haxe will inline and eliminate it.
This is awesome and much more readable than creating fields manually for some cases.
ref https://haxe.org/manual/macro-reification-class.html
TODO: Make article about this, and/or integrate in those https://code.haxe.org/tag/building-fields.html