代码之家  ›  专栏  ›  技术社区  ›  Alexey Romanov

Scala反射中的消除失真类型

  •  2
  • Alexey Romanov  · 技术社区  · 11 年前

    如何解析给定的别名 Type ? 即。

    import reflect.runtime.universe._
    
    type Alias[A] = Option[Option[A]]
    val tpe = typeOf[Alias[_]] 
    val ExistentialType(quantified, underlying) = tpe
    

    我怎么得到 Option[Option[_$1]] 从…起 underlying (或来自 tpe )? 我知道 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)
    
    1 回复  |  直到 11 年前
        1
  •  6
  •   Alexey Romanov    11 年前

    结果调用了该方法 normalize 第2.10节(已弃用和 dealias 在2.11中添加)。不知道我是如何在第一次搜索时错过它的。

    推荐文章