Blog
-
Posted on Jun 17 2012
In case you don't know, Haxe has a power macro system which allows you to do a lot of compile-time checking and code generation. It's a really neat feature that enable you to extend the language in many different ways while still keeping the same syntax and being strictly typed.
But up to now it was quite difficult to write macros, since you had to construct the AST (which is the piece of Haxe code you want to generate) by-hand with enums. For instance let's say you want to generate a String "Hello World", you would have to write :
@:macro static function hello() { var pos = haxe.macro.Context.currentPosition(); return { expr : EConst(CString("Hello World")), pos : pos }; }
And for a more complex
for( x in 0...n )loop :// repeat the expression 'e' for 'n' times @:macro static function repeat( e : Expr, n : Int ) { var pos = haxe.macro.Context.currentPosition(); // 0...n var ezero = { expr : EConst(CInt(0)), pos : pos }; var eN = { expr : EConst(CInt...
(more...)12 comments