scala泛型函数将“Any”类型转换为指定的泛型类型参数

时间:2017-01-14 15:44:50

标签: scala generics types casting

是否可以编写接受“Any”类型的值的泛型函数和泛型类型参数说“T”并通过检查传入的值的运行时类型返回Option [T]?

我在repl中尝试过(Scala版本2.11.7(Java HotSpot(TM)64位服务器VM,Java 1.8.0_66))

def cast[T](x: Any): Option[T] = x match {
case v: T => Some(v)
case _ =>  Option.empty[T]
}

我收到警告:

 warning: abstract type pattern T is unchecked since it is eliminated by erasure
           case v: T => Some(v)

我如何传递类类型并进行显式类型检查而不是泛型?

P.S。显然由于类型擦除,上面的代码爆炸了。样本repl交互:

scala> val x :Any = 123                                                                                    
x: Any = 123                                                                                               

scala> cast[Int](x)                                                                                        
res0: Option[Int] = Some(123)                                                                              

scala> cast[String](x)                                                                                     
res1: Option[String] = Some(123)                                                                           

scala> val x :Any = "dfg"                                                                                  
x: Any = dfg                                                                                               

scala> cast[Int](x)                                                                                        
res2: Option[Int] = Some(dfg)                                                                              

scala> cast[Int](x).get                                                                                    
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer                         
  at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:101)                                          
  ... 33 elided   

1 个答案:

答案 0 :(得分:4)

运行时类型需要ClassTag

def cast[T: ClassTag](x: Any): Option[T] = x match {
  case v: T => Some(v)
  case _ =>  Option.empty[T]
}

println(cast[Int](1))
println(cast[Int]("hello"))

收率:

Some(1)
None