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

who likes writing def equals?

1 reply
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

Nobody! What if some sort of leprechaun could write your equals and
hashCode methods and all you had to do was pay them a little manifest?

// notice only x passed to superclass as value equality basis
class Measurement[T: Manifest](x: Int, y: Int) extends util.EqualsHelperClass[T](x)

class Velocity
class Distance
class SuperVelocity extends Velocity

val x1 = new Measurement[Velocity](5, 0)
val x2 = new Measurement[Distance](5, 0)
val x3 = new Measurement[SuperVelocity](5, 0)
val x4 = new Measurement[Velocity](5, 5150)

x1 != x2 // not same type
x1 != x3 // nope, subtypes do not confuse us
x1 == x4 // winner

Or skip the values:

scala> EqualsHelper[List[List[Int]]] == EqualsHelper[List[List[String]]]
res7: Boolean = false

scala> EqualsHelper[List[List[Int]]] == EqualsHelper[List[List[Int]]]
res8: Boolean = true

I'm not checking it in though because my excitement limit has been
exceeded for a while. If anyone wants to look it over and anticipate
issues, branch and source file are at:

http://github.com/paulp/scala/tree/equalsHelper
http://tinyurl.com/y98dymu

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: who likes writing def equals?
Interesting. I may have to revise my article on equality. :-)

On Wed, Nov 4, 2009 at 8:10 PM, Paul Phillips <paulp@improving.org> wrote:
Nobody! What if some sort of leprechaun could write your equals and
hashCode methods and all you had to do was pay them a little manifest?

 // notice only x passed to superclass as value equality basis
 class Measurement[T: Manifest](x: Int, y: Int) extends util.EqualsHelperClass[T](x)

 class Velocity
 class Distance
 class SuperVelocity extends Velocity

 val x1 = new Measurement[Velocity](5, 0)
 val x2 = new Measurement[Distance](5, 0)
 val x3 = new Measurement[SuperVelocity](5, 0)
 val x4 = new Measurement[Velocity](5, 5150)

 x1 != x2  // not same type
 x1 != x3  // nope, subtypes do not confuse us
 x1 == x4  // winner

Or skip the values:

 scala> EqualsHelper[List[List[Int]]] == EqualsHelper[List[List[String]]]
 res7: Boolean = false

 scala> EqualsHelper[List[List[Int]]] == EqualsHelper[List[List[Int]]]
 res8: Boolean = true

I'm not checking it in though because my excitement limit has been
exceeded for a while.  If anyone wants to look it over and anticipate
issues, branch and source file are at:

 http://github.com/paulp/scala/tree/equalsHelper
 http://tinyurl.com/y98dymu

--
Paul Phillips      | Giving every man a vote has no more made men wise
Analgesic          | and free than Christianity has made them good.
Empiricist         |     -- H. L. Mencken
ha! spill, pupil   |----------* http://www.improving.org/paulp/ *----------



--
Daniel C. Sobral

Veni, vidi, veterni.

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