HaxeDoc2

(やまだぃちぅ) #1

9.2.2 Constant Expressions


A macro can be declared to expect constant (5.2) arguments:
1 class Main {
2 static public function main() {
3 const("foo", 1, 1.5, true);
4 }
5
6 macro static function
7 const(s:String, i:Int, f:Float,b:Bool){
8 trace(s);
9 trace(i);
10 trace(f);
11 trace(b);
12 return macro null;
13 }
14 }


With these it is not necessary to detour over expressions as the compiler can use the provided
constants directly.

9.2.3 Rest Argument


If the final argument of a macro is of typeArray, the macro accepts an arbitrary number
of extra arguments which are available from that array:
1 import haxe.macro.Expr;
2
3 class Main {
4 static public function main() {
5 myMacro("foo", a, b, c);
6 }
7
8 macro static function
9 myMacro(e1:Expr, extra:Array) {
10 for (e in extra){
11 trace(e);
12 }
13 return macro null;
14 }
15 }


9.3 Reification


The Haxe Compiler allowsreificationof expressions, types and classes to simplify working with
macros. The syntax for reification ismacro expr, whereexpris any valid Haxe expression.

9.3.1 Expression Reification


Expression reification is used to create instances ofhaxe.macro.Exprin a convenient way.
The Haxe Compiler accepts the usual Haxe syntax and translates it to an expression object. It
supports several escaping mechanisms, all of which are triggered by the$character:
Free download pdf