Skip to content

Instantly share code, notes, and snippets.

@Simn
Last active December 11, 2015 07:09
Show Gist options
  • Save Simn/4564731 to your computer and use it in GitHub Desktop.
Save Simn/4564731 to your computer and use it in GitHub Desktop.
Build + reification example
import haxe.macro.Context;
import haxe.macro.Expr;
class Builder {
macro static function build():Array<Field> {
var funcExpr = macro function():String {
return "test";
}
var ctorExpr = macro function(foo) {
trace("I was created with " +foo);
}
return [makeFunc("test", funcExpr), makeFunc("new", ctorExpr)];
}
static function makeFunc(name:String, e:Expr, ?access, ?meta) {
return {
name: name,
doc: null,
access: access != null ? access : [APublic],
pos: e.pos,
meta: meta != null ? meta : [],
kind: switch(e.expr) {
case EFunction(_, func): FFun(func);
case _: Context.error("Argument to makeFunc must be a function expression", e.pos);
}
}
}
}
class Main {
private static function main() {
trace(new BuildMe("Argument").test());
}
}
@:build(Builder.build())
class BuildMe { }
@freewind
Copy link

Thank you, Simn!

@jasononeil
Copy link

Nice simple example. Created a version with some comments in case it helps anyone: https://gist.github.com/4570788

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