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

The state of equality

5 replies
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.

Hi all,

I had a major change of heart in almost the last minute before the
countdown to 2.8 is set off. Equality will largely stay as it is,
except that in some cases == should be much faster than it was before.
Equality is now spread out over two methods.

BoxesRuntime.equals contains the full equality test (but much
streamlined wrt to what's in there for 2.7).
ScalaRuntime.inlinedEquals is called (and inlined) instead if
-optimize is set. This contains a fast path for the most common case.

== does not always correspond to hashCode, because hashCode is
brain-dead for longs. Instead there is a new method

hash(x: Any)

in Predef which provides a hash that is compatible with the result of
==. So from Scala code you should in the future always use
x == y and hash(z), where from Java code you used x.equals(y) and
y.hashCode. The hash method in Predef is also inlined under
-optimize which should make it quite fast.

Thanks to Paul for not giving up trying to convince me and to Jesper
for suggesting the Predef.hash solution.

Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: The state of equality

On Mon, Nov 09, 2009 at 02:09:22PM +0100, martin odersky wrote:
> I had a major change of heart in almost the last minute before the
> countdown to 2.8 is set off.

If this were a movie, what music would be playing right now?

a) We Are The Champions
b) Bittersweet Symphony
c) Eye of the Tiger
d) ...

> hash(x: Any)

Can you elaborate on why this is a method in Predef and not on Any? Is
it to avoid conflict with pre-existing methods? I suppose that's a good
enough reason, but a little unfortunate.

What I did last night was actually to steal hashCode() away from java
and rewrite the results. Not that I think that's the right way to go
(although it was kind of neat to watch) but x.hash() feels more like
scala than hash(x).

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: The state of equality


On Mon, Nov 9, 2009 at 2:19 PM, Paul Phillips <paulp@improving.org> wrote:

On Mon, Nov 09, 2009 at 02:09:22PM +0100, martin odersky wrote:
> I had a major change of heart in almost the last minute before the
> countdown to 2.8 is set off.

If this were a movie, what music would be playing right now?

 a) We Are The Champions
 b) Bittersweet Symphony
 c) Eye of the Tiger
 
d) http://www.youtube.com/watch?v=LTnq268y2ms  :-)



> hash(x: Any)

Can you elaborate on why this is a method in Predef and not on Any? Is
it to avoid conflict with pre-existing methods? I suppose that's a good
enough reason, but a little unfortunate.

What I did last night was actually to steal hashCode() away from java
and rewrite the results.  Not that I think that's the right way to go
(although it was kind of neat to watch) but x.hash() feels more like
scala than hash(x).

--
Paul Phillips      | Adultery is the application of democracy to love.
Apatheist          |     -- H. L. Mencken
Empiricist         |
slap pi uphill!    |----------* http://www.improving.org/paulp/ *----------



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: twitter.com/viktorklang
Code: github.com/viktorklang
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: The state of equality

On Mon, Nov 9, 2009 at 2:19 PM, Paul Phillips wrote:
>
> On Mon, Nov 09, 2009 at 02:09:22PM +0100, martin odersky wrote:
>> I had a major change of heart in almost the last minute before the
>> countdown to 2.8 is set off.
>
> If this were a movie, what music would be playing right now?
>
>  a) We Are The Champions
>  b) Bittersweet Symphony
>  c) Eye of the Tiger
>  d) ...
>
>> hash(x: Any)
>
> Can you elaborate on why this is a method in Predef and not on Any? Is
> it to avoid conflict with pre-existing methods? I suppose that's a good
> enough reason, but a little unfortunate.
>
> What I did last night was actually to steal hashCode() away from java
> and rewrite the results.  Not that I think that's the right way to go
> (although it was kind of neat to watch) but x.hash() feels more like
> scala than hash(x).
>
Two reasons: (1) Less magic. Hash is a normal method; no compiler
support needed.
(2) We can make use of overloading to make hash even faster for Ints,
Longs and Numbers.
That said, I don't have that strong an opinion about this.

Cheers

ijuma
Joined: 2008-08-20,
User offline. Last seen 22 weeks 2 days ago.
Re: The state of equality

Hi all,

On Mon, 2009-11-09 at 14:33 +0100, martin odersky wrote:
> On Mon, Nov 9, 2009 at 2:19 PM, Paul Phillips wrote:
> >
> > On Mon, Nov 09, 2009 at 02:09:22PM +0100, martin odersky wrote:
> >> I had a major change of heart in almost the last minute before the
> >> countdown to 2.8 is set off.
> >
> > If this were a movie, what music would be playing right now?
> >
> > a) We Are The Champions
> > b) Bittersweet Symphony
> > c) Eye of the Tiger
> > d) ...
> >
> >> hash(x: Any)
> >
> > Can you elaborate on why this is a method in Predef and not on Any? Is
> > it to avoid conflict with pre-existing methods? I suppose that's a good
> > enough reason, but a little unfortunate.
> >
> > What I did last night was actually to steal hashCode() away from java
> > and rewrite the results. Not that I think that's the right way to go
> > (although it was kind of neat to watch) but x.hash() feels more like
> > scala than hash(x).
> >
> Two reasons: (1) Less magic. Hash is a normal method; no compiler
> support needed.
> (2) We can make use of overloading to make hash even faster for Ints,
> Longs and Numbers.
> That said, I don't have that strong an opinion about this.

I think it would be nice if hash was a method on Any in the same way ==
is. To me, hash is to hashCode as == is to equals, so it would be nice
if the approach used for both was consistent.

Best,
Ismael

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: The state of equality

d) http://en.wikipedia.org/wiki/The_Beat_Goes_On

On Mon, Nov 9, 2009 at 2:32 PM, Ismael Juma wrote:
> Hi all,
>
> On Mon, 2009-11-09 at 14:33 +0100, martin odersky wrote:
>> On Mon, Nov 9, 2009 at 2:19 PM, Paul Phillips wrote:
>> >
>> > On Mon, Nov 09, 2009 at 02:09:22PM +0100, martin odersky wrote:
>> >> I had a major change of heart in almost the last minute before the
>> >> countdown to 2.8 is set off.
>> >
>> > If this were a movie, what music would be playing right now?
>> >
>> >  a) We Are The Champions
>> >  b) Bittersweet Symphony
>> >  c) Eye of the Tiger
>> >  d) ...
>> >
>> >> hash(x: Any)
>> >
>> > Can you elaborate on why this is a method in Predef and not on Any? Is
>> > it to avoid conflict with pre-existing methods? I suppose that's a good
>> > enough reason, but a little unfortunate.
>> >
>> > What I did last night was actually to steal hashCode() away from java
>> > and rewrite the results.  Not that I think that's the right way to go
>> > (although it was kind of neat to watch) but x.hash() feels more like
>> > scala than hash(x).
>> >
>> Two reasons: (1) Less magic. Hash is a normal method; no compiler
>> support needed.
>> (2) We can make use of overloading to make hash even faster for Ints,
>> Longs and Numbers.
>> That said, I don't have that strong an opinion about this.
>
> I think it would be nice if hash was a method on Any in the same way ==
> is. To me, hash is to hashCode as == is to equals, so it would be nice
> if the approach used for both was consistent.
>
> Best,
> Ismael
>
>

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