2

I have the following code:

@compileTimeOnly("enable macro paradise to expand macro annotations")
class typedTable[T] extends StaticAnnotation {
  def macroTransform(annottees: Any*): Table = macro TableGenerator.impl[T]
}

object TableGenerator {
  def impl[T](c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Table] = {
    import c.universe._
    val tpe = weakTypeOf[T]  // This doesn't work
    ...
  }
}

And I can't seem to figure out how to access the TypeTag for 'T' in a Macro Annotation. I've seen other posts that make vague reference to how it can be accessed but I can't seem to figure out exactly how I'm supposed to access it in this context.

darkfrog
  • 1,053
  • 8
  • 29

1 Answers1

3

Macro annotations don't typecheck their arguments, which means that you can only get trees from these arguments, not their types. In order to obtain the trees that represent type arguments of your macro annotation, call c.macroApplication and destructure it appropriately.

Eugene Burmako
  • 13,028
  • 1
  • 46
  • 59
  • I apologize, but I'm still very new to Macros, can you explain a little bit more what I need to do? I want to iterate over the fields in the primary constructor presuming T is a case class. – darkfrog Jul 18 '15 at 13:15
  • Take a look at this: http://stackoverflow.com/questions/19379436/cant-access-parents-members-while-dealing-with-macro-annotations, and hopefully that'll answer your question. Please feel free to ask more if you get stuck. – Eugene Burmako Jul 19 '15 at 07:21