Aladdin - Scala Bugtracking
[#290] project: interpreter priority: low category: bug
submitter assigned to status date submitted
Nikolay Philippe fixed 2004-02-02 18:47:30.0
subject [contrib #14] too tolerant cast
code
> val x:Any = "Toto"
val x: scala.Any = Toto
> x.asInstanceOf[int]
Toto: scala.Int
what happened
String "Toto" has been considered as an integer
what expected An exception !
[back to overview]
Changes of this bug report
Nikolay  edited on  2004-02-02 18:51:34.0
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)
        ...
Philippe  edited on  2004-03-24 12:49:30.0
Added code to correctly handle isInstanceOf and asInstanceOf in the interpreter.