-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compare productPrefix in synthetic case class canEqual #21606
base: main
Are you sure you want to change the base?
Conversation
|
||
object Test extends App { | ||
val c = C(1) | ||
assert(c != new D) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a semantic change, in 3.5.0 and 2.13.15, c == new D
is true.
Since 2.13, case class `hashCode` mixes in the hash code of the `productPrefix` string. The synthetic `equals` method has fallen out of sync in that regard, so two instances can be equal but have different hash codes. This commit changes the synthetic `canEqual` method to also compare the `productPrefix` of the two instances.
@sjrd pointed out that the change introduces this discrepancy: scala> case class C(x: Int)
scala> class D extends C(1) { override def productPrefix = "D" }
scala> new D match { case C(1) => true }
val res0: Boolean = true
scala> new D == C(1)
val res1: Boolean = false |
What it fixes is this: scala> case class C(x: Int)
scala> class D extends C(1) { override def productPrefix = "D" }
scala> val s = collection.mutable.HashSet(C(1))
scala> s.contains(new D)
val res0: Boolean = false
scala> s.exists(_ == new D)
val res1: Boolean = true |
Have we considered removing Should we perhaps use a "hard-coded" seed instead (computed at compile-time from the hash code of the full class name, for example)? |
Sorry, I missed your reply. I initially suggested that on private chat, but @retronym pointed out that then case class hash codes would be inconsistent with |
I don't think that's a problem. Case classes don't compare equal to other |
Since 2.13, case class
hashCode
mixes in the hash code of theproductPrefix
string. The syntheticequals
method has fallen out of sync in that regard, so two instances can be equal but have different hash codes.This commit changes the synthetic
canEqual
method to also compare theproductPrefix
of the two instances.Scala 3 forward port of scala/scala#10859, for scala/bug#13033.