HaxeDoc2

(やまだぃちぅ) #1
17 }

18
19 static function makeEnumField(name, kind){
20 return {
21 name: name,
22 doc: null,
23 meta: [],
24 access: [],
25 kind: kind,
26 pos: Context.currentPos()
27 }
28 }
29 }


1 @:build(EnumBuildingMacro.build())
2 enum E {}
3
4 class Main {
5 static public function main() {
6 switch(E.A){
7 case A:
8 case B(v):
9 }
10 }
11 }


Because enumEis annotated with a:buildmetadata, the called macro builds two construc-
torsAandB“into” it. The former is added with the kind beingFVar(null, null), meaning
it is a constructor without argument. For the latter, we use reification (9.3.1) to obtain an instance
ofhaxe.macro.Expr.Functionwith a singularIntargument.
Themainmethod proves the structure of our generated enum by matching (6.4) it. We can
see that the generated type is equivalent to this:
1 enum E {
2 A;
3 B(value:Int);
4 }

9.5.2 @:autoBuild


If a class has the:autoBuildmetadata, the compiler generates:buildmetadata on all ex-
tending classes. If an interface has the:autoBuildmetadata, the compiler generates:build
metadata on all implementing classes and all extending interfaces. Note that:autoBuilddoes
not imply:buildon the class/interface itself.
1 import haxe.macro.Context;
2 import haxe.macro.Expr;
3
4 class AutoBuildingMacro {
5 macro static public
6 function fromInterface():Array<Field>{
7 trace("fromInterface: "
8 + Context.getLocalType());
Free download pdf