如何在宏中将短类型名称解析为完整类型名称 [英] How do I resolve a short type name to a full type name in a Macro
本文介绍了如何在宏中将短类型名称解析为完整类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在宏内部有没有办法使用当前上下文来完全扩展类型名称?例如:
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"
推荐答案
您的宏可能会在包含任意 import prefix.Foo
的树中展开,因此您要问是否可以查询封闭树:如果我发出一个名称 Foo
,你会如何对它进行类型检查?
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
就是你的答案.
val t = c.typeCheck(q"??? : Foo").tpe.typeSymbol.fullName
或在 2.11 中使用 c.typecheck
.
or use c.typecheck
in 2.11.
或者,如果您找不到 scaladoc...
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)
}
Travis Brown Eugene Burmakro [原文如此] 在你需要他的时候在哪里?
Where is Travis Brown Eugene Burmakro [sic] when you need him?
这篇关于如何在宏中将短类型名称解析为完整类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文