如何将变量转换为某种运行时类型从Scala中的TypeCast [英] How to cast a variable to certain runtime type got from TypeCast in Scala

查看:126
本文介绍了如何将变量转换为某种运行时类型从Scala中的TypeCast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

asInstanceOf [T] 适用于简单情况,其中类型 T 例如

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

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

scala> val m1:Any = Map(a→1,b→2)其中,a,b,c,

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 [A​​ny,Any]]
res52:Map [A​​ny,Any] = Map(a - > 1,2 - > b)
/ pre>

但是当运行时通过 TypeTags T c $ c>, 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 不是类型 。因此 reflect.runtime.universe.Type 不是真实的类型?如何使用 typeTag 信息将值转换为某种类型?

解决方案

intT 不是类型,而是类型的实例。混乱,肯定。我不确定是否有一些内置的反射API这样做,但你可以很容易地写一个方法,将转换为 A 给定一个元素任何 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


The asInstanceOf[T] works for "simple" case where the type T is given explicitly by code: e.g.

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)

But when the type T is retrieved at runtime through TypeTags, asInstanceOf[T] does not work. For example:

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]

The error says it clear that intT is not a type. So reflect.runtime.universe.Type is not a real type? How to cast a value to certain type using typeTag information?

解决方案

intT isn't a type, but an instance of Type. Confusing, for sure. I'm not sure if there is something built into the reflection API that does this, but you can easily write a method that will cast to A given an element Any and a 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

Another variant using an implicit class:

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

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

这篇关于如何将变量转换为某种运行时类型从Scala中的TypeCast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆