This page is no longer maintained — Please continue to the home page at www.scala-lang.org

"Unpredictable" class cast exception in Seq.sortWith

5 replies
Niels Hoogeveen
Joined: 2010-02-08,
User offline. Last seen 42 years 45 weeks ago.

I'm trying sort a sequence of tuples based on one of the tuple elements which
is a Double and run into a class cast exception.

I am not able to create a simplified version of this problem, since the
exception isn't always raised. It seems to depend on the order of
compilation.

I am using the Eclipse plug-in version 2.8.0 RC3.

When running a clean built of the project, the exceptions is raised.
Touching the file which contains the sortWith afterwards and saving it,
recompiles that particular file, and after that when running my program the
exception is not raised.

Here is a partial stack trace of the exception I am encountering.

java.lang.ClassCastException: java.lang.Double cannot be cast to
java.lang.Integer
at scala.runtime.BoxesRunTime.unboxToInt(Unknown Source)
at
org.poundpuplegacy.graphweb.presentation.CompositeTemplate$$anonfun$1.apply(CompositeTemplate.scala:65)
at
org.poundpuplegacy.graphweb.presentation.CompositeTemplate$$anonfun$1.apply(CompositeTemplate.scala:65)
at scala.math.Ordering$$anon$4.compare(Ordering.scala:127)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at scala.collection.SeqLike$class.sorted(SeqLike.scala:828)
at scala.collection.immutable.List.sorted(List.scala:45)
at scala.collection.SeqLike$class.sortWith(SeqLike.scala:786)
at scala.collection.immutable.List.sortWith(List.scala:45)

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: "Unpredictable" class cast exception in Seq.sortWith

On Sat, Jun 19, 2010 at 02:53:24PM -0700, Niels Hoogeveen wrote:
> I'm trying sort a sequence of tuples based on one of the tuple
> elements which is a Double and run into a class cast exception.

I'm not sure how that is happening, and if you can narrow it you should
probably open a ticket, but in the meantime: you are calling sortWith,
which takes a (T, T) => Boolean argument, and one way or another it
thinks (Int, Int) => Boolean is the right thing. It's the closure being
compiled to CompositeTemplate$$anonfun$1.apply.

If you insist it use (Double, Double) => Boolean I expect your CCE will
go away. As always it's a little easier if you supply some source.
Most likely it would be enough to see the sortWith.

Niels Hoogeveen
Joined: 2010-02-08,
User offline. Last seen 42 years 45 weeks ago.
Re: "Unpredictable" class cast exception in Seq.sortWith

> If you insist it use (Double, Double) => Boolean I expect your CCE will
> go away. As always it's a little easier if you supply some source.
> Most likely it would be enough to see the sortWith.
>
The offending line of code reads like this:

val sortedChldtRels = chldtRels.sortWith((a, b) =>
a.get(SubtemplateWeight).asInstanceOf[Double] <
b.get(SubtemplateWeight).asInstanceOf[Double])

The get(SubtemplateWeight) calls return a Double, but since the dependent
method type (running with both Xfuture and Xexperimental on) I use for the
get method doesn't always get inferred properly, I explicitly cast it to a
Double in the comparison closure.

The underlying value of get(SubtemplateWeight) is most certainly a Double,
or to be more accurate a java.lang.Double.

I wish I could reproduce the error, but it seems to be a pretty intricate
error that depends on the order of compilation. Once I recompile the file
containing the offending line of code, the problem goes away.

SubtemplateWeight and the get method are both not contained in the file
giving this CCE.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: "Unpredictable" class cast exception in Seq.sortWith

On Sun, Jun 20, 2010 at 05:17:05AM -0700, Niels Hoogeveen wrote:
> The offending line of code reads like this:
>
> val sortedChldtRels = chldtRels.sortWith((a, b) =>
> a.get(SubtemplateWeight).asInstanceOf[Double] <
> b.get(SubtemplateWeight).asInstanceOf[Double])
>
> The get(SubtemplateWeight) calls return a Double, but since the
> dependent method type (running with both Xfuture and Xexperimental on)

Oh I see, only two explicit casts, two nutty compiler options, and one
very experimental and unsupported feature. Do I have to even explain
what's wrong with this question and answer sequence? You make me feel
like a sucker for trying to answer the original question.

Perhaps I can illustrate by analogy.

"Doctor, there's something wrong with my child! He has a terrible head
pain!"

"I'm a medical professional, I'm here to help. Do you have any idea
what could have happened?"

"Not a one! He's been quietly hitting himself in the forehead with the
hammer from his Little Mechanic toolset and I've been feeding him these
experimental brain pills I bought from a Nigerian pharmacy, but I don't
know what that could have to do with anything."

"Thank you for paging me. I prescribe amputation."

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: Re: "Unpredictable" class cast exception in Seq.sortWith
If I ever write a Scala book, can I please quote you on that?It's inspired, and really must be preserved for future generations...
:)
On 20 June 2010 17:03, Paul Phillips <paulp [at] improving [dot] org> wrote:
On Sun, Jun 20, 2010 at 05:17:05AM -0700, Niels Hoogeveen wrote:
> The offending line of code reads like this:
>
> val sortedChldtRels = chldtRels.sortWith((a, b) =>
> a.get(SubtemplateWeight).asInstanceOf[Double] <
> b.get(SubtemplateWeight).asInstanceOf[Double])
>
> The get(SubtemplateWeight) calls return a Double, but since the
> dependent method type (running with both Xfuture and Xexperimental on)

Oh I see, only two explicit casts, two nutty compiler options, and one
very experimental and unsupported feature.  Do I have to even explain
what's wrong with this question and answer sequence? You make me feel
like a sucker for trying to answer the original question.

Perhaps I can illustrate by analogy.

"Doctor, there's something wrong with my child! He has a terrible head
pain!"

"I'm a medical professional, I'm here to help.  Do you have any idea
what could have happened?"

"Not a one! He's been quietly hitting himself in the forehead with the
hammer from his Little Mechanic toolset and I've been feeding him these
experimental brain pills I bought from a Nigerian pharmacy, but I don't
know what that could have to do with anything."

"Thank you for paging me.  I prescribe amputation."

--
Paul Phillips      | We must respect the other fellow's religion, but only
Everyman           | in the sense and to the extent that we respect his
Empiricist         | theory that his wife is beautiful and his children smart.
i pull his palp!   |     -- H. L. Mencken



--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Niels Hoogeveen
Joined: 2010-02-08,
User offline. Last seen 42 years 45 weeks ago.
Re: "Unpredictable" class cast exception in Seq.sortWith

Thank you for your kind response. It was most enlightening.

Somewhere down the line, dependent method types will no longer be
experimental and then this issue will probably pop up in other peoples code
too, until that time I will have to live with a somewhat quirky compilation
cycle.

Sorry I wasted your precious time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland