为什么Manifest被弃用了?我何时应该使用ClassTag,何时应该使用TypeTag

时间:2017-11-25 17:32:29

标签: scala

我在ManifestTypeTag上有几个问题。据我所知,JVM不了解泛型并删除类型。所以我不能这样做

def factoryForAll[T] = new T // will not compile. Runtime doesn't know what T is

Scala编译器可以使用Manifest(现已弃用)将有关类型的信息传输到运行时。 Manifest包含erasure等方法,其中包含有关类型的信息。所以我可以按照以下方式创建泛型T

的对象
def factoryForall[T](implicit ev:Manifest[T]) = ev.erasure.newInstance

scala> factoryForAll[String]
res1:Any=""

scala> class C
defined class C

scala> factoryForAll[C]
res5: Any = C@52cb52bd

问题1 - 有趣的是,它对Int(或Float)不起作用?为什么?

scala> factoryForAll[Int]
java.lang.InstantiationException: int

问题2 - 为什么Manives会被弃用?我了解新版本TypeTag有更丰富的信息,但我不明白Manifest中的缺点

问题3 - Scala 2.12仍有Manifest类(https://www.scala-lang.org/api/current/scala/reflect/Manifest.html)。如果Manifest不好,为什么Scala还有呢?文档指的是使用Manifest创建Arrays通用类型,但数组也可以由ClassTag实现。那么为什么Scala仍有Manifest

scala> def makeArray[T](len:Int)(implicit ev:ClassTag[T]) = new Array[T](len)
makeArray: [T](len: Int)(implicit ev: scala.reflect.ClassTag[T])Array[T]

scala> makeArray[String](4)
res39: Array[String] = Array(null, null, null, null)

scala> makeArray[Int](4)
res40: Array[Int] = Array(0, 0, 0, 0)

scala> val al = makeArray[List[Int]](2)
al: Array[List[Int]] = Array(null, null)

scala> al(0) = List(1)

scala> al(1) = List(2,3)

来到TypeTag,有3种类型。参考Scala文档(http://docs.scala-lang.org/overviews/reflection/typetags-manifests.html)和关于Medium(https://medium.com/@sinisalouc/overcoming-type-erasure-in-scala-8f2422070d20)的教程,我了解TypeTagClassTag具有不同的用例。 ClassTag无法区分超出第一级擦除的类型。

//method to extract a type from a collection
def extractType[T](col:Iterable[Any])(implicit ev:ClassTag[T]) = {
val it =col.iterator
while (it.hasNext) {
val el = it.next
el match {
case x:T => println("got T")
case _ => println("not T")
}}}

extractType: [T](col: Iterable[Any])(implicit ev: scala.reflect.ClassTag[T])Unit

scala> extractType[Int](List(1,2,3,"hello"))
got T
got T
got T
not T

scala> extractType[List[Int]](List(List(1),List(2),List(3),List("hello")))
got T
got T
got T
got T //this should be not T

问题4:如果ClassTag无法区分第一级别的删除,当我尝试在String中添加List[Set[Int]]时,为什么会出现以下错误?是不是Int已被删除?

scala> def makeArray[T](len:Int)(implicit ev:ClassTag[T]) = new Array[T](len)
makeArray: [T](len: Int)(implicit ev: scala.reflect.ClassTag[T])Array[T]

scala> val al = makeArray[List[Set[Int]]](2)
al: Array[List[Set[Int]]] = Array(null, null)

scala> al(0) = List(Set(2))

scala> al(1) = List(Set("2"))
<console>:28: error: type mismatch;
 found   : String("2")
 required: Int
       al(0) = List(Set("2"))
                        ^

问题5 - 为什么在前面的示例extractType[List[Int]](List(List(1),List(2),List(3),List("hello")))中,Scala无法将StringInt区分开,但它区分了al(0) = List(Set(2))al(1) = List(Set("2"))

问题6 - 如何更改extractType功能,以便检查嵌入类型。我知道我必须使用TypeTag,但我不知道如何检查集合中元素的类型。

def extractType[T](col:Iterable[Any])(implicit ev:TypeTag[T]) = {
    println("class is "+ev.mirror.runtimeClass) //I suppose in TypeTag, runtime is here
    val it =col.iterator
    while (it.hasNext) {
    val el = it.next
    el match {
        case x:T => println("got T") //this doesn't compile. What should I check for?
        case _ => println("not T")
    }}}

1 个答案:

答案 0 :(得分:1)

问题1:IntFloat不是由JVM中的类表示的(尽管它们具有相应的Class个对象),更不用说具有无参数构造函数的对象了。尽管名称中有forAll,但这适用于非常有限的一组类型。

问题2:Manifest混淆了ClassTagTypeTag分开的问题。

问题3:如果你看一下Manifest的来源,就说

// TODO undeprecated until Scala reflection becomes non-experimental 
// @deprecated("use scala.reflect.ClassTag (to capture erasures) or scala.reflect.runtime.universe.TypeTag (to capture types) or both instead", "2.10.0") 

问题4/5:此错误来自静态类型al: Array[List[Set[Int]]]。不涉及ClassTagTypeTag提供的运行时信息。

问题6:只使用标准库而不能使用。但请参阅Shapeless

相关问题