Scala 反射中的去混叠类型
Dealiasing Types in Scala reflection
如何解析给定 Type
的别名?即
import reflect.runtime.universe._
type Alias[A] = Option[Option[A]]
val tpe = typeOf[Alias[_]]
val ExistentialType(quantified, underlying) = tpe
如何从 underlying
(或 tpe
)得到 Option[Option[_]]
?我知道 typeSymbol
确实解析了别名,但它似乎在这个过程中丢失了参数:
scala> val tT = typeOf[Alias[_]].typeSymbol
tT: reflect.runtime.universe.Symbol = class Option
scala> tT.asType.toType
res3: reflect.runtime.universe.Type = Option[A]
scala> tT.asType.typeParams
res4: List[reflect.runtime.universe.Symbol] = List(type A)
该方法在 2.10 中被调用为 normalize
(已弃用并在 2.11 中添加了 dealias
)。不知道我是怎么在第一次搜索时错过它的。
如何解析给定 Type
的别名?即
import reflect.runtime.universe._
type Alias[A] = Option[Option[A]]
val tpe = typeOf[Alias[_]]
val ExistentialType(quantified, underlying) = tpe
如何从 underlying
(或 tpe
)得到 Option[Option[_]]
?我知道 typeSymbol
确实解析了别名,但它似乎在这个过程中丢失了参数:
scala> val tT = typeOf[Alias[_]].typeSymbol
tT: reflect.runtime.universe.Symbol = class Option
scala> tT.asType.toType
res3: reflect.runtime.universe.Type = Option[A]
scala> tT.asType.typeParams
res4: List[reflect.runtime.universe.Symbol] = List(type A)
该方法在 2.10 中被调用为 normalize
(已弃用并在 2.11 中添加了 dealias
)。不知道我是怎么在第一次搜索时错过它的。