How do I resolve a short type name to a full type name in a Macro

馋奶兔 提交于 2020-01-14 01:57:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!