Re: weird asInstanceOf issue

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: weird asInstanceOf issue

Iulian Dragos-2
On Tue, 2006-01-10 at 17:35 -0500, Michael Benfield wrote:

> I'm getting a ClassCastException in a very specific situation when
> trying to convert a float to an int with asInstanceOf:
>
> -----------
>
> $ cat MyTest.scala
>
> object MyTest {
>      def main(args: Array[String]) = {
>          var f: float = 0f;
>          for(val x <- Iterator.range(0, 0))
>              f = f + 1.5f;
>          val i: int = f.asInstanceOf;
>          System.out.println(i);
>      }
>
> }
> $ scalac MyTest.scala
> $ scala MyTest
> Exception in thread "main" java.lang.ClassCastException
>          at MyTest$.main(MyTest.scala:7)
>          at MyTest.main(MyTest.scala:3)
>
> ----------
>
> If I change "asInstanceOf" to "toInt", it works. What's weirdest is

'asInstanceOf' takes one type parameter, the type you want to convert
to. So your code should be:

val i: Int = f.asInstanceOf[Int];

and that works fine for me. The ClassCastException appears because the
compiler infers the missing type parameter to be scala.All. By the way,
one can see that by running scalac with '-print:refcheck'. I have no
idea why it infers it to be scala.All, but I assume it takes the most
specific type that could possibly go there..

> that the for loop isn't doing anything, but if I remove it, it works.
> If I change the for loop so that its body doesn't involve f, it works.

I could not reproduce this behavior with the newest (beta) release of
Scala. For the current scalac release, it has to do with a bug in
boxing. The for loop is actually a closure in disguise, so there is
quite a lot of work done to ensure that the closure may modify f (it is
replaced by a reference cell to a float, you can see that by adding '-
print:lambda'). That's why removing the loop helps.

Cheers,
Iulian


Reply | Threaded
Open this post in threaded view
|

Re: weird asInstanceOf issue

Keith Hodel
Iulian Dragos <iulian.dragos <at> epfl.ch> writes:

> 'asInstanceOf' takes one type parameter, the type you want to convert
> to. So your code should be:
>
> val i: Int = f.asInstanceOf[Int];
>
> and that works fine for me. The ClassCastException appears because the
> compiler infers the missing type parameter to be scala.All. By the way,


I had tried the following

     val i: int = f.asInstanceOf[Int];

and that didn't work. Iulian's suggestion doesn't work for me either; I still
get the ClassCastException (using version 1.4.0.3). Maybe this is a moot point
since the new version of the compiler is in beta, but I thought I'd mention it
anyway.

Keith Hodel



Reply | Threaded
Open this post in threaded view
|

Re: weird asInstanceOf issue

Iulian Dragos-2
On Thu, 2006-01-12 at 17:40 +0000, Keith Hodel wrote:

> Iulian Dragos <iulian.dragos <at> epfl.ch> writes:
>
> > 'asInstanceOf' takes one type parameter, the type you want to convert
> > to. So your code should be:
> >
> > val i: Int = f.asInstanceOf[Int];
> >
> > and that works fine for me. The ClassCastException appears because the
> > compiler infers the missing type parameter to be scala.All. By the way,
>
>
> I had tried the following
>
>      val i: int = f.asInstanceOf[Int];
>
> and that didn't work. Iulian's suggestion doesn't work for me either; I still
> get the ClassCastException (using version 1.4.0.3). Maybe this is a moot point

Indeed, that works only for the new beta. With Scala 1.4 it throws a
cast exception (but without the type parameter, the two compilers behave
the same way).

Iulian