考虑以下特征:
sealed trait Test
//Test's branches
trait Base {
type Action = Test
}
现在我需要将 ClassSymbol
的 Test
称为 Base#Action
。
这是我的尝试:
def macroImpl[B <: Base: c.WeakTypeTag](c: blackbox.Context): c.Tree = {
import c.universe._
val baseType = weakTypeOf[B].typeSymbol.asType
val actionType = c.typecheck(tq"$baseType#Action", mode = c.TYPEMode)
println(tq"$actionType") //prints pack.age.Base#Action
println(tq"$actionType".symbol) //prints type Action
println(tq"$actionType".symbol.asClass) //raises scala.ScalaReflectionException: type Action is not a class
}
就我而言,Base#Action = Test
这绝对是一个类。
有没有办法通过类型投影从宏实现中引用它的 ClassSymbol
?
答案 0 :(得分:2)
首先尝试取树的类型
tq"$actionType".tpe.typeSymbol.asClass