Skip to content

Class reification for building fields #96

@markknol

Description

@markknol

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 something is just a syntax sugar for creating data structures (Expr, TypeDefinition, ComplexType, etc), so of course if you create macro 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions