I think that the interpreter doesn't, in fact, perform the cast (whatever that means in the context of the interpreter) because if we type the lines from the example followed by
> val y: Any = x.asInstanceOf[int]
val y: scala.Any = Toto
However, if we specify the type of the val as int (or leave it to type inference),
it crashes with an exception:
> val z: int = x.asInstanceOf[int]
Exception in thread "main" scalac.ApplicationError:
object = null
method = java.lang.reflect.Method(public static int scala.runtime.RunTime.unbox_ivalue(scala.Int))
args = [Toto]
at scalac.util.Debug.abort(Debug.java:62)
at scala.tools.scalai.Evaluator.invoke(Evaluator.java:359)
at scala.tools.scalai.Evaluator.invoke(Evaluator.java:249)
at scala.tools.scalai.Evaluator.evaluate(Evaluator.java:189)
at scala.tools.scalai.Evaluator.evaluate(Evaluator.java:180)
at scala.tools.scalai.Evaluator.evaluate(Evaluator.java:141)
at $console$9.$console$9(:1)
at .$console$9(:1)
Caused by java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at scala.tools.scalai.Evaluator.invoke(Evaluator.java:341)
at scala.tools.scalai.Evaluator.invoke(Evaluator.java:249)
at scala.tools.scalai.Evaluator.evaluate(Evaluator.java:189)
at scala.tools.scalai.Evaluator.evaluate(Evaluator.java:180)
...
|