def testLong(v: Any): Unit = {
println(v.getClass) // print: class java.lang.Long
}
testLong(103L) // compile passed
from last snippet, it shows that the variable v
is class java.lang.Long
.And it's a subclass of java.lang.Object
.
We also know from the scala Unified types system the AnyRef
is equals to java.lang.Object
type. But why it compile failed as follows:
def testLong(v: AnyRef): Unit = {
println(v.getClass)
}
testLong(103L) // compile failed