Skip to content

Instantly share code, notes, and snippets.

@nadako
Created March 9, 2021 17:13
Show Gist options
  • Save nadako/273497023683e20f964ccd6ac0643422 to your computer and use it in GitHub Desktop.
Save nadako/273497023683e20f964ccd6ac0643422 to your computer and use it in GitHub Desktop.
import haxe.macro.Context;
import haxe.macro.Expr;
function build():Array<Field> {
var fields = Context.getBuildFields();
for (field in fields) {
switch [field.name, field.kind] {
case ["new", FFun(ctor)]:
var assignExprs = [];
for (arg in ctor.args) {
switch (arg.meta) {
case [{name: "this"}]:
var fieldName = arg.name;
assignExprs.push(macro this.$fieldName = $i{arg.name});
case _:
}
}
var assigns = macro $b{assignExprs};
ctor.expr = if (ctor.expr == null) assigns else macro { $assigns; ${ctor.expr}; };
case _:
}
}
return fields;
}
@:build(DI.build())
class Person {
public var name:String;
public function new(@this name);
}
function main() {
var p = new Person("Dan");
trace(p.name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment