How do I get the ("current") type of a macro annottee?
import scala.annotation.StaticAnnotation
import scala.reflect.macros._
import language.experimental.macros
class myself extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro myselfMacro.impl
}
object myselfMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val a = annottees.head
println(s"static type = ${a.staticType}")
println(s"actual type = ${a.actualType}")
c.Expr[Any](Literal(Constant()))
}
}
Test:
@myself class Foo
Output:
static type = Nothing
actual type = null
The reason I want that type is that I want to use it as a type parameter, e.g. Bar[Foo]
Edit:
Ok, so I think the correct approach is like this:
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val v = annottees.head.asInstanceOf[ClassDef]
val tpe = v.tpe // <- tpe is null as the annotated type is not yet type checked!
val tpe2 = if (tpe == null)
c.typeCheck(v).tpe // <- fails with a compiler error (assertion failure)
else
tpe
println(s"Type of annottee: $tpe2")
???
}
But given this post by Eugene Burmako, it looks like it is currently not possible...