如何比较 Scala 反射库类型“universe.Type"和普通类型之间的公平性? [英] How to compare equity between Scala reflection library type `universe.Type` and an ordinary type?
本文介绍了如何比较 Scala 反射库类型“universe.Type"和普通类型之间的公平性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在练习使用 Scala 反射功能,结果如下:
I'm practicing to use Scala reflection features, I get this result:
res46: reflect.runtime.universe.Type = scala.List[String]
如何测试结果值是否代表一个List[String]
?
How to test the result value see if it represents a List[String]
?
换句话说,如何测试一个universe.Type
是否代表一个指定的普通Scala类型?
In another word, how to test whether a universe.Type
represents a specified ordinary Scala type?
推荐答案
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]]
这篇关于如何比较 Scala 反射库类型“universe.Type"和普通类型之间的公平性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文