如何将变量转换为某些运行时类型从Scala中的TypeCast获取

时间:2015-04-01 19:22:03

标签: scala types casting

asInstanceOf[T]适用于“简单”情况,其中类型T由代码明确指定:例如

scala> val x: Any = 5
x: Any = 5

scala> x.asInstanceOf[Int]
res50: Int = 5

scala> val m1: Any = Map[String, Int]("a"->1, "b"->2)
m1: Any = Map(a -> 1, b -> 2)

scala> m.asInstanceOf[Map[String, Int]]
res51: Map[String,Int] = Map(a -> 1, 2 -> b)

scala> val m2: Any = Map[Any,Any]("a"->1, 2->"b")
m2: Any = Map(a -> 1, 2 -> b)

scala> m.asInstanceOf[Map[Any, Any]]
res52: Map[Any,Any] = Map(a -> 1, 2 -> b)

但是当在运行时通过T检索到类型TypeTags时,asInstanceOf[T]不起作用。例如:

scala> val intT = typeTag[Int].tpe
intT: reflect.runtime.universe.Type = Int

scala> x.asInstanceOf[intT]
<console>:12: error: not found: type intT
              x.asInstanceOf[intT]

错误表明intT不是type。那么reflect.runtime.universe.Type不是真正的type?如何使用typeTag信息将值转换为特定类型?

1 个答案:

答案 0 :(得分:10)

intT不是类型,而是Type的实例。令人困惑,当然。我不确定反射API中是否有内置功能可以执行此操作,但您可以轻松编写一个方法,在给定元素AAny的情况下将转换为TypeTag[A]

def cast[A](a: Any, tt: TypeTag[A]): A = a.asInstanceOf[A]

scala> val intT = typeTag[Int]
intT: reflect.runtime.universe.TypeTag[Int] = TypeTag[Int]

scala> val a: Any = 1
a: Any = 1

scala> cast(a, intT)
res101: Int = 1

另一个使用隐式类的变体:

implicit class TypeTagCast(a: Any) {
    def fromTypeTag[A](tt: TypeTag[A]): A = a.asInstanceOf[A]
 }

scala> a.fromTypeTag(intT)
res106: Int = 1