【问题标题】:How to compare equity between Scala reflection library type `universe.Type` and an ordinary type?如何比较 Scala 反射库类型“universe.Type”和普通类型之间的公平性?
【发布时间】:2013-11-01 18:23:10
【问题描述】:

我正在练习使用 Scala 反射功能,我得到了这个结果:

res46: reflect.runtime.universe.Type = scala.List[String]

如何测试结果值是否代表List[String]

换句话说,如何测试universe.Type是否代表指定的普通Scala类型?

【问题讨论】:

    标签: scala reflection


    【解决方案1】:
    import scala.reflect.runtime.universe._
    
    val tpe: Type = ???
    
    //type equivalence test (is tpe exactly List[String]?)
    tpe =:= typeOf[List[String]]
    
    //type conformance test (is tpe a subtype of List[String]?)
    tpe <:< typeOf[List[String]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多