我對Manifest
和TypeTag
有幾個問題。我瞭解JVM不知道泛型並刪除類型。所以,我不能這樣做Manifest爲何不推薦使用?什麼時候應該使用ClassTag,何時應該使用TypeTag
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 = [email protected]
問題1的對象 - 有趣的是,它不適合詮釋工作(或浮動)?爲什麼?
scala> factoryForAll[Int]
java.lang.InstantiationException: int
問題2 - 爲什麼是明顯過時?據我所知,新的版本,TypeTag
具有更豐富的信息,但我不明白什麼是缺點的清單
問題3 - 斯卡拉2.12仍然有Manifest
類(https://www.scala-lang.org/api/current/scala/reflect/Manifest.html)。如果Manifest
不好,爲什麼斯卡拉仍然有它?該文檔涉及使用Manifest
創建Generic類型的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)和中等教程(https://medium.com/@sinisalouc/overcoming-type-erasure-in-scala-8f2422070d20),我知道TypeTag
和ClassTag
有不同的用例。 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
無法擦除的1級,爲什麼會收到以下錯誤,當我嘗試在List[Set[Int]]
添加String
區分。是不是刪除了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"))
^
Question5 - 爲什麼在前面的例子extractType[List[Int]](List(List(1),List(2),List(3),List("hello")))
,斯卡拉無法區分Int
String
但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")
}}}