Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class reification for building fields #96

Open
markknol opened this issue Jan 2, 2018 · 0 comments
Open

Class reification for building fields #96

markknol opened this issue Jan 2, 2018 · 0 comments

Comments

@markknol
Copy link
Member

markknol commented Jan 2, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant