Dans le code suivant:Pourquoi Scala fait la promotion de List [Any] sur List [Long]?
def test(list: List[Any]): Unit = {
list.foreach {
v =>
v match {
case r: AnyRef => println(r + ": " + r.getClass.getName)
case d: Double => println(d + ": Double")
case f: Float=> println(f + ": Float")
case b: Byte => println(b + ": Byte")
case c: Char => println(c + ": Char")
case s: Short => println(s + ": Short")
case i: Int => println(i + ": Int")
case l: Long=> println(l + ": Long")
case b: Boolean => println(b + ": Boolean")
case _ => throw new IllegalArgumentException("Unknown type: " + v)
}
}
}
test(List(0L, 1.asInstanceOf[Int], 2.asInstanceOf[Short]))
ici est la sortie (Scala 2.8.1):
0: java.lang.Long
1: java.lang.Long
2: java.lang.Long
Pourquoi les chiffres sont promus à java.lang.Long? Comment est-ce que je peux faire ceci de sorte qu'ils gardent leurs types AnyVal
ou soient "encadrés" au type équivalent AnyRef
?