I have been playing with basic Scala data types. I noticed that the scala.Any
class defines the method asInstanceOf[T0]: T0
from here The API has i
Just done more tests(compiled and run), I think this may be from one bug of asInstanceOf, it won't do cast.
The last line command gives me compiling error:
Warning:(46, 38) fruitless type test: a value of type Int cannot also be a S println(listTemp2(1).isInstanceOf[S]) // won't compile
^
val listOfS = Some(List(S("i1", "k1", "s1"), S("i2", "k2", "s2")))
val listTemp:Seq[K] = listOfS.get.asInstanceOf[Seq[K]]
val listTemp2:Seq[Int] = listOfS.get.asInstanceOf[Seq[Int]]
println("ListTemp:")
println(listTemp(1)) //print S(i2,k2,s2)
println(listTemp(1).isInstanceOf[S]) // true
println(listTemp(1).isInstanceOf[K]) // false
println(listTemp2(1)) //print S(i2,k2,s2)
println(listTemp2(1).isInstanceOf[Int]) // false
println(listTemp2(1).isInstanceOf[S]) // won't compile
Java (and Scala) allows you to cast a primitive double
to int
(in Scala's case, Double
to Int
). On the other hand, you cannot cast java.lang.Double
to java.lang.Int
.
When you declare the Double
as an Any
, you're explicitly asking the compiler to forget that you gave it a Double
. Therefore, in order to support the Any
interface, the compiler is storing the value as a boxed double (i.e., java.lang.Double
).
The behavior does seem confusing, but it's not a bug. According §12.1 of the Scala Language Spec:
The test x.asInstanceOf[T] is treated specially if T is a numeric value type (§12.2). In this case the cast will be translated to an application of a conversion method x.toT (§12.2.1).
I think you've confused the terms "cast" and "convert".
The standard conversion methods begin with to
, e.g. 20d.toInt
will convert a value 20
of type Double
a value 20 of type Int
.
asInstanceOf
on the other hand is a special type casting method. All it does is informs the compiler that the value is of the type specified in its parameter, if during runtime the value on which you call this method does not match with what you specified in the type parameter, you'll get an exception thrown. I.e. in a.asInstanceOf[B]
the provided value a
must be of a type B
or inherit from it - otherwise you'll get an exception.