Last active
December 11, 2015 07:09
-
-
Save Simn/4564731 to your computer and use it in GitHub Desktop.
Build + reification example
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; | |
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); | |
} | |
} | |
} | |
} |
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
class Main { | |
private static function main() { | |
trace(new BuildMe("Argument").test()); | |
} | |
} | |
@:build(Builder.build()) | |
class BuildMe { } |
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
Thank you, Simn!