Why doesn't Case Class synthetic equals use ScalaRunTime._equals?

110 views
Skip to first unread message

cao...@gmail.com

unread,
Sep 26, 2014, 8:21:05 AM9/26/14
to scala...@googlegroups.com
Hi,
I've been playing around with case classes a bit and discovered this SO post


and this gist reimplementing a case class manually,


except that what I found from JADing a case class is that "ScalaRunTime._equals" isn't used for equality even though _hashCode/_toString and therefore the Product trait (and productArity/productElement) are used for toString and hashCode.

Is there any reason for this? 

Thanks,

Caoilte

Antoine Gourlay

unread,
Sep 30, 2014, 6:24:26 AM9/30/14
to cao...@gmail.com, scala-user

Hmm, that’s interesting. Actually ScalaRunTime._equals hasn’t been used by case classes for a very long time:

It was used in Scala until v2.6, when it got disabled (16d3cf1) and then completely removed in v2.8 (76294e0). I don’t think that method is used anywhere now,

For reference, the code that generates the current implementation of equals is at SyntheticMethods.scala:203. The method’s scaladoc describes fairly well what you’ll get.

As to the reason for the change, those commit don’t mention anything…

The old implementation used productArity and productElement at runtime to enumerate the content of the case class. Maybe this was to remove the direct dependency on Product (case classes that don’t extend Product? who knows…), or to support people overriding those methods (although that’s madness)…

Antoine


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Zaugg

unread,
Sep 30, 2014, 9:32:09 AM9/30/14
to Antoine Gourlay, cao...@gmail.com, scala-user
On Tue, Sep 30, 2014 at 8:24 PM, Antoine Gourlay <ant...@gourlay.fr> wrote:

Hmm, that’s interesting. Actually ScalaRunTime._equals hasn’t been used by case classes for a very long time:

It was used in Scala until v2.6, when it got disabled (16d3cf1) and then completely removed in v2.8 (76294e0). I don’t think that method is used anywhere now,

For reference, the code that generates the current implementation of equals is at SyntheticMethods.scala:203. The method’s scaladoc describes fairly well what you’ll get.

As to the reason for the change, those commit don’t mention anything…

The old implementation used productArity and productElement at runtime to enumerate the content of the case class. Maybe this was to remove the direct dependency on Product (case classes that don’t extend Product? who knows…), or to support people overriding those methods (although that’s madness)…

I believe this is performance driven. Using the generic x.productIterator sameElements y.productIterator
 can be quite a bit slower, especially if it incurs boxing.

-jason

Reply all
Reply to author
Forward
0 new messages