Accessing Metadata on other classes within a Macro

145 views
Skip to first unread message

theRemix

unread,
Oct 29, 2013, 4:40:42 AM10/29/13
to haxe...@googlegroups.com
I don't know much about how to use macros, and i'm using haxe 2.10 at the moment

I'm going off of this guide http://haxe.org/manual/metadata
haxe.rtti.Meta won't work here
and i'm not sure how to actually access field metadata from Types in a macro context

import haxe.macro.Expr;

class MyClass {
  @:mydata(123) var myvar:String;
  
}

class MacroMeta {
  @:macro public static function getMeta() : Expr {
    var m = haxe.rtti.Meta.getFields(MyClass);
    trace(m);
    return { expr : EConst(CString("")), pos : haxe.macro.Context.currentPos() };
  }

  public static function main():Void{
    getMeta();
  }
  
}

the trace output:
MacroMeta.hx:11: {}

what is the correct way to access the metadata on the fields of another class?

Juraj Kirchheim

unread,
Oct 29, 2013, 5:42:24 AM10/29/13
to haxe...@googlegroups.com
Use Context.resolve("full.path.ClassName") to get the Type, which with
a bit of luck is TInst. Then get the ClassType from that, then iterate
through the fields to find the one you're looking for, and then access
the metadata.

Regards,
Juraj

theRemix

unread,
Oct 29, 2013, 6:21:40 PM10/29/13
to haxe...@googlegroups.com
Thanks for your guidance, however i don't understand how to do what you say.

Use Context.resolve("full.path.ClassName") to get the Type

there is no Context.resolve, do you mean Context.resolvePath( file : String ) : String ? or Context.getType( name : String ) : Type ?

Then get the ClassType from that
How do i get the ClassType? i'm not familiar with macros

this is what i have, and this doesn't work
@:macro public static function getMeta() : Expr {
    var t = haxe.macro.Context.getType("MyClass");
    // trace(t); // TInst(MyClass,[])
    var cl = Type.getClass(t);
    trace(cl); // null
    return { expr : EConst(CString("")), pos : haxe.macro.Context.currentPos() };
  }

Juraj Kirchheim

unread,
Oct 29, 2013, 7:26:15 PM10/29/13
to haxe...@googlegroups.com
Yes, sorry. I meant Context.getType.

You would use a switch to get to the class:

switch haxe.macro.Context.getType("MyClass") {
case TInst(ref, _):
var cl:ClassType = ref.get();
case default:
}

Regards,
Juraj
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/groups/opt_out.

Confidant

unread,
Oct 21, 2015, 4:08:26 PM10/21/15
to Haxe
I am also trying to access metadata from within a macro without much luck. My simplified code:


@:build(
   
MacroClass.theMethod("cms/application/cms.php")
)
class GeneratedExtern implements Dynamic { }

class Main
{
@libdirLive("/path/to/lib") var ld1:String;

}

//in a separate file:
class MacroClass{
 macro
static public function theMethod(filePath:String){
  switch Context.getType("Main") {
     
case TInst(ref, params):

         
var cl:ClassType = ref.get();

         trace
(Meta.getFields(cl));
     
default:
  }

 }
}



outputs:
src/MacroClass.hx:85: {}
src/MacroClass.hx:85: {}
src/MacroClass.hx:85: {}
src/MacroClass.hx:85: {}
src/MacroClass.hx:85: {}
src/MacroClass.hx:85: {}
src/MacroClass.hx:85: {}

What should I do?

Juraj Kirchheim

unread,
Oct 21, 2015, 4:20:18 PM10/21/15
to haxe...@googlegroups.com
Ok, here is the basic issue: you are trying to get the metadata through `haxe.rtti.Meta`. Unfortunately, the "r" in "rtti" stands for "runtime", so it is the wrong tool for the job. However, the solution is actually even simpler:

  trace(cl.meta.get());//traces the metadata of the class
  for (field in cl.fields.get())
    trace(field.meta.get());//traces the metadata of each field

I hope that clarifies things.

Best,
Juraj

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Confidant

unread,
Oct 22, 2015, 11:02:47 AM10/22/15
to Haxe
Always ask the experts. :) Thanks—it works of course!
Reply all
Reply to author
Forward
0 new messages