使用 Scala 2.10 反射的类型参数的运行时解析 [英] Runtime resolution of type arguments using scala 2.10 reflection
问题描述
给定类型声明,我能够解析类型参数.
Given a type declaration, I am able to resolve the type argument.
scala> reflect.runtime.universe.typeOf[List[Int]] match {case x:TypeRef => x.args}
res10: List[reflect.runtime.universe.Type] = List(Int)
对于运行时值,同样的方法不起作用.
For a runtime value, The same method doesn't work.
scala> reflect.runtime.currentMirror.reflect(List(42)).symbol.toType match {case x:TypeRef => x.args}
res11: List[reflect.runtime.universe.Type] = List(B)
有没有办法克服反射值的类型擦除?
Is there a way to overcome the type erasure for reflected values?
推荐答案
一个基于阅读TypeTag知识的例子Scala:什么是TypeTag以及如何使用它?发布者Eugene Burmako 在对您的问题的评论中:
An example based on TypeTag knowledge gained from reading Scala: What is a TypeTag and how do I use it? posted by Eugene Burmako in the comments on your question:
import scala.reflect.runtime.universe._
object ScalaApplication {
def main(args: Array[String]) {
printType(List(42))
printType(List("42"))
printType(List("42", 42))
}
def printType[T : TypeTag](t: T) {
println(typeOf[T])
}
}
这应该给出输出:
$ scala ScalaApplication.scala
List[Int]
List[String]
List[Any]
[更新 1:]
但是,如果您想知道分配给 Any
类型的引用的类型,您可能必须选择某种类型感知包装器:
However, if you want to be aware of the type assigned to a reference of type Any
you might have to opt for some sort of type aware wrapper:
import scala.reflect.runtime.universe._
object ScalaApplication {
def main(args: Array[String]) {
val anyWrapper = new AnyWrapper
List(1,2,3).foreach { i =>
i match {
case 1 => anyWrapper.any = 42
case 2 => anyWrapper.any = "a string"
case 3 => anyWrapper.any = true
}
print(anyWrapper.any)
print(" has type ")
println(anyWrapper.typeOfAny)
}
}
class AnyWrapper {
private var _any: Any = null
private var _typeOfAny: Type = null
def any = _any
def typeOfAny = _typeOfAny
def any_=[T: TypeTag](a: T) = {
_typeOfAny = typeOf[T]
_any = a
}
}
}
这应该给出输出:
$ scala ScalaApplication.scala
42 has type Int
a string has type String
true has type Boolean
但是这个解决方案仍然没有涵盖在编译时引用类型未知的情况.
But this solution still does not cover the case where the reference type is unknown at compile time.
[更新 2:]
如果类型被显式转换为类型 Any
的引用,您可能必须在匹配语句中枚举所有可能的类型以恢复类型:
If the types are explicitly cast to reference of type Any
, you might have to enumerate all the possible types in a match statement in order to recover the type:
import scala.reflect.runtime.universe._
object ScalaApplication {
def main(args: Array[String]) {
List(1,2,3).foreach { i =>
val any: Any = i match {
case 1 => 42.asInstanceOf[Any]
case 2 => "a string".asInstanceOf[Any]
case 3 => true.asInstanceOf[Any]
}
print(any)
print(" has type ")
println(matchType(any))
}
}
def matchType(any: Any) = {
any match {
case a: Int => typeOf[Int]
case a: String => typeOf[String]
case a: Boolean => typeOf[Boolean]
}
}
}
这应该给出输出:
$ scala ScalaApplication.scala
42 has type Int
a string has type String
true has type Boolean
但此解决方案要求您知道(并列出)您可以在 any
值中接收的所有可能类型.
But this solution requires you to know (and list) all the possible types that you could receive in the any
value.
这篇关于使用 Scala 2.10 反射的类型参数的运行时解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!