Why is ClassManifest needed with Array but not List?

前端 未结 4 1445
长情又很酷
长情又很酷 2020-12-29 09:49

Define the following code:

import scala.collection.JavaConversions._  
val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator
def func(a:Any):String         


        
4条回答
  •  不知归路
    2020-12-29 10:40

    Method toArray creates new array with elements of type T. And according to documentation:

    So depending on the actual type parameter for T, this could be an Array[Int], or an Array[Boolean], or an array of some of the other primitive types in Java, or an array of some reference type. But these types have all different runtime representations, so how is the Scala runtime going to pick the correct one? In fact, it can't do that based on the information it is given, because the actual type that corresponds to the type parameter T is erased at runtime.

提交回复
热议问题