-
-
Save jasononeil/4570788 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; | |
class Builder { | |
// Build macros are called using @:build() or @:autoBuild() metadata | |
// They must return an array containing all of the fields of the new type | |
macro static function build():Array<Field> { | |
// Create expression representing the test() function | |
var funcExpr = macro function():String { | |
return "test"; | |
} | |
// Create expression representing the new() constructor function | |
var ctorExpr = macro function(foo) { | |
trace("I was created with " +foo); | |
} | |
// Return an array of the fields we want in our class - the test() and new() methods | |
return [makeFunc("test", funcExpr), makeFunc("new", ctorExpr)]; | |
} | |
// This takes the expression representing a function (e) and the name and turns it into | |
// a Field declaration. Access and meta are optional, defaulting to "public" and empty | |
// metadata, respectively. | |
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 : [], | |
// Make sure the expression given is in fact a function, and then declare that | |
// our field is a function (FFun) defined inside our given expression. | |
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() { | |
// Create a new BuildMe object, which though it is empty, will have new() and test() created by the build macro | |
trace(new BuildMe("Argument").test()); | |
} | |
} | |
@:build(Builder.build()) | |
class BuildMe { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment