Skip to content

Case class hashCode #7

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

Closed
scabug opened this issue Aug 28, 2007 · 2 comments
Closed

Case class hashCode #7

scabug opened this issue Aug 28, 2007 · 2 comments
Assignees

Comments

@scabug
Copy link

scabug commented Aug 28, 2007

Aladdin: [http://scala-webapps.epfl.ch/bugtracking/bugs/displayItem.do?id=1293 bug 1293], [http://scala-webapps.epfl.ch/bugtracking/contribs/display.do?id=775 contrib 775]

== Code ==

The language specification has
"Method hashCode: ()int computes a hash-code depending on the data
structure in a way which maps equal (with respect to equals) values to equal
hash-codes."

This is only true if the hashCodes of constructor arguments have this property (see example below).

Some possible alternative wordings:-

"Method hashCode: ()int computes a hash-code depending on the data
structure in a way which maps equal (with respect to equals) values to equal
hash-codes, provided the hashCode methods of the data structure members each map equal
(with respect to equals) values to equal hash-codes."

"Method hashCode: ()int computes a hash-code. If the hashCode methods of the data structure members map
equal (with respect to equals) values to equal hash-codes, then the case class hashCode method does too."

object BadlyBehaved
{
    var counter = 0
}

class BadlyBehaved
{
    BadlyBehaved.counter = BadlyBehaved.counter + 1

    override val hashCode = BadlyBehaved.counter

    override def equals(rhs: Any) = rhs.isInstanceOf[BadlyBehaved]
}

case class CounterExample(b: BadlyBehaved) {}

val first = CounterExample(new BadlyBehaved())
val second = CounterExample(new BadlyBehaved())
assert(first.b == second.b)
assert(first.b.hashCode != second.b.hashCode)
assert(first == second)
assert(first.hashCode == second.hashCode, "Language Spec and implementation disagree") // FAILS

== What happened ==

java.lang.AssertionError: assertion failed: Language Spec and implementation disagree

== What expected ==

@scabug
Copy link
Author

scabug commented Aug 28, 2007

Imported From: https://issues.scala-lang.org/browse/SI-7?orig=1
Reporter: @michelou

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone 2.6.0 deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants