问题
Inside a macro is there a way of using the current Context to fully expand a type name? Eg something like:
context.resolveShortTypeNameToFullTypeName("Foo") = "com.acme.Foo"
回答1:
Your macro might expand in a tree that includes an arbitrary import prefix.Foo
, so you're asking if you can query that enclosing tree: If I emit a name Foo
, how would you typecheck it?
symbol.fullName
is your answer.
val t = c.typeCheck(q"??? : Foo").tpe.typeSymbol.fullName
or use c.typecheck
in 2.11.
or, if you can't find the scaladoc...
val k = c.asInstanceOf[scala.reflect.macros.contexts.Context]
locally {
import k.universe._
val n = k.callsiteTyper.typed(q"??? : Foo").tpe.typeSymbol.fullName
println(n)
}
Where is Travis Brown Eugene Burmakro [sic] when you need him?
来源:https://stackoverflow.com/questions/21683971/how-do-i-resolve-a-short-type-name-to-a-full-type-name-in-a-macro