Skip to content

Instantly share code, notes, and snippets.

@Simn
Created February 19, 2014 18:29
Show Gist options
  • Save Simn/9098351 to your computer and use it in GitHub Desktop.
Save Simn/9098351 to your computer and use it in GitHub Desktop.
initLocals macro
class Main {
var arg1:Int;
var arg2:String;
var arg3:Bool;
function new(arg1 = 0, arg2 = "foo", arg3 = false) {
MyMacro.initLocals();
}
static public function main() {
var m = new Main();
trace(m.arg1, m.arg2, m.arg3);
}
}
import haxe.macro.Expr;
import haxe.macro.Context;
using Lambda;
class MyMacro {
macro static public function initLocals() {
var locals = Context.getLocalVars();
var fields = Context.getLocalClass().get().fields.get();
var exprs = [];
for (local in locals.keys()) {
if (fields.exists(function(field) return field.name == local)) {
exprs.push(macro this.$local = $i{local});
}
}
var eInit = macro $b{exprs};
return eInit;
}
}
@AxGord
Copy link

AxGord commented Mar 2, 2014

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