Advertisement
Guest User

macros

a guest
Sep 30th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.24 KB | None | 0 0
  1.  
  2. import haxe.macro.Context;
  3. import haxe.macro.Expr;
  4.  
  5. class TestMacro {
  6.  
  7.     static var allFields = [];
  8.  
  9.     public macro static function generateClass()
  10.     {
  11.         var c = macro class MyClass {
  12.             public function new() { }
  13.         }
  14.  
  15.         for(field in allFields)
  16.         {
  17.             c.fields.push({
  18.                     name: field.name,
  19.                     doc: null,
  20.                     meta: [],
  21.                     access: [APublic],
  22.                     kind: FVar(field.type, null),
  23.                     pos: Context.currentPos()
  24.                 });
  25.         }
  26.         haxe.macro.Context.defineType(c);
  27.  
  28.         return macro null;
  29.     }
  30.  
  31.     macro static public function build():Array<Field> {
  32.  
  33.         var fields = Context.getBuildFields();
  34.  
  35.         for (field in fields)
  36.         {
  37.             switch (field.kind)
  38.             {
  39.                 case FVar(type, _):
  40.                     switch (type)
  41.                     {
  42.                         case TPath(path):
  43.  
  44.                             allFields.push({name:field.name,type:type});
  45.                            
  46.                         default:
  47.                     }
  48.                 default:
  49.             }
  50.         }
  51.         return fields;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement