Eq Type Class in Scala?

160 views
Skip to first unread message

Seyed H. HAERI (Hossein)

unread,
May 31, 2012, 6:57:32 AM5/31/12
to scala-user
Dear all,

Does Scala have anything like the Eq type class of Haskell which can
mitigate the big hassle of implementing the correct equals method for
case classes?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------

Matthew Tyler

unread,
May 31, 2012, 7:06:04 AM5/31/12
to Seyed H. HAERI (Hossein), scala-user

Case classes provide the equals method. Is this insufficient for your requirements?

Seyed H. HAERI (Hossein)

unread,
May 31, 2012, 7:12:37 AM5/31/12
to scala-user
Hi Matthew,

> Case classes provide the equals method.

Oh, do they? And, that's structural equivalence like Eq of Haskell?

Matthew Tyler

unread,
May 31, 2012, 7:14:01 AM5/31/12
to Seyed H. HAERI (Hossein), scala-user

I believe so.

Josh Suereth

unread,
May 31, 2012, 7:15:52 AM5/31/12
to Seyed H. HAERI (Hossein), scala-user

Yes.  Case classes do implement both equals and hashCode by structure.

Seyed H. HAERI (Hossein)

unread,
May 31, 2012, 7:16:33 AM5/31/12
to scala-user
Cool! Would you mind referring me to the part of spec which says that?
--

Lukas Rytz

unread,
May 31, 2012, 7:23:03 AM5/31/12
to Seyed H. HAERI (Hossein), scala-user
end of 5.3.2

Seyed H. HAERI (Hossein)

unread,
May 31, 2012, 7:25:33 AM5/31/12
to scala-user
Great! Thanks. :)

Tony Morris

unread,
May 31, 2012, 7:28:56 AM5/31/12
to Seyed H. HAERI (Hossein), scala-user

scalaz.Equal

Josh Suereth

unread,
May 31, 2012, 7:32:45 AM5/31/12
to Tony Morris, scala-user, Seyed H. HAERI, (Hossein)

Except it doesn't have structural autogen yet...

Tony Morris

unread,
May 31, 2012, 4:49:18 PM5/31/12
to Josh Suereth, scala-user, Seyed H. HAERI, (Hossein)

Sure it does. It just looks a lot different and is flexible.

Equal composes.
Equal[A] => Equal[B] => Equal[B]

Combined with lenses, the effort required is proportional to the deviaton from the default. That is, the default requires no effort (is "generated").

Josh Suereth

unread,
May 31, 2012, 5:01:35 PM5/31/12
to Tony Morris, scala-user, Seyed H. HAERI, (Hossein)
Right.  I guess I meant with less typing.   Someday soon, I hope, typeclass will have less boilerplate in Scala.   We have to wait for the new features to stabilize before adding more though....

Tony Morris

unread,
May 31, 2012, 5:11:25 PM5/31/12
to Josh Suereth, scala-user, Seyed H. HAERI, (Hossein)
If we are indulging in our desires of the language in this respect, I
might just add, in a rather loud voice, newtype/deriving.
--
Tony Morris
http://tmorris.net/


Raoul Duke

unread,
May 31, 2012, 5:13:59 PM5/31/12
to scala-user
On Thu, May 31, 2012 at 2:11 PM, Tony Morris <tonym...@gmail.com> wrote:
> If we are indulging in our desires of the language in this respect, I
> might just add, in a rather loud voice, newtype/deriving.

yes, please, and how.

it always kills me that some such isn't obviously required in any
statically typed languages for it to be considered "real". :-}

Seyed H. HAERI (Hossein)

unread,
May 31, 2012, 6:58:25 PM5/31/12
to scala-user
Guys,

And, in case I want a different equality than the structural one, is
that possible to override equals for a case class?

TIA,
--Hossein

On 31 May 2012 13:23, Lukas Rytz <lukas...@epfl.ch> wrote:

Grzegorz Balcerek

unread,
May 31, 2012, 10:13:50 PM5/31/12
to Seyed H. HAERI (Hossein), scala-user
Yes, it is.

scala> case class A(a: Int) { override def equals(x:Any) = true }
defined class A

scala> A(1) == A(2)
res0: Boolean = true

Grzegorz

missingfaktor

unread,
Jun 1, 2012, 1:10:05 AM6/1/12
to Seyed H. HAERI (Hossein), scala-user
And if you are using the type-class approach, just add a new implicit instance and have it in scope.

implicit object FooEqual extends Equal[Foo] {
  def equal(a: Foo, b: Foo) = true
--
Cheers,

Tony Morris

unread,
Jun 1, 2012, 6:29:42 PM6/1/12
to Josh Suereth, scala-user, Seyed H. HAERI, (Hossein)

Correction.

Equal composes (to records).
Equal[A] => Equal[B] => Equal[(A,B)]

Reply all
Reply to author
Forward
0 new messages