-
-
Save nadako/273497023683e20f964ccd6ac0643422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@: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