【问题标题】:Dealiasing Types in Scala reflectionScala 反射中的去锯齿类型
【发布时间】:2015-03-29 03:17:30
【问题描述】:

如何解决给定Type 的别名?即

import reflect.runtime.universe._

type Alias[A] = Option[Option[A]]
val tpe = typeOf[Alias[_]] 
val ExistentialType(quantified, underlying) = tpe

如何从underlying(或tpe)获得Option[Option[_$1]]?我知道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)

【问题讨论】:

    标签: scala reflection scala-reflect


    【解决方案1】:

    该方法在 2.10 中被称为 normalize(已弃用并在 2.11 中添加了 dealias)。不知道我是如何在第一次搜索时错过它的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      相关资源
      最近更新 更多