Skip to content
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

Remove "Chain#hashCode is consistent with List#hashCode" #4718

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rossabaker
Copy link
Member

It's no longer true as of Scala 2.13.16, and was probably overspecified in the first place.

Fixes #4716.

@armanbilge
Copy link
Member

armanbilge commented Feb 21, 2025

was probably overspecified in the first place.

Agree with this.

It's no longer true as of Scala 2.13.16

Isn't that because they fixed a hashing bug, that we may want to fix too?

Edit (aha!):

Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the over-specification did help us keep in sync with upstream changes, so maybe that's worth something, but IMO, not enough.

@rossabaker
Copy link
Member Author

That's a fair point. I don't care about equality, but being alerted to the improved hash algorithm was nice. I'd also be willing to leave it with a comment so it's less of a chase for the next maintainer who hits this.

@satorg
Copy link
Contributor

satorg commented Feb 21, 2025

Honestly, I'm not sure I'm following how come it was assumed that

Chain#hashCode is consistent with List#hashCode

in the first place? What was the justification for that? I mean, Chain and List are different types, they don't have to have same hash codes even if they contain same elements, or do they?

@rossabaker
Copy link
Member Author

They don't need the same hash codes unless they're equal, which is not a goal, shouldn't be a goal, and would be quite a challenge to do lawfully even if it were a goal.

@johnynek
Copy link
Contributor

I think the original idea is that it is easy to mess up a hash and make it too weak. Also it would be easy to make a structural hash to cares about the construction order which we don't want. In this case we had a test that asserts the hashCode is the same as toList.hashCode which proves it can't be a terrible hash (it can't have a few of the failure modes).

So it's not a terrible idea but the weakness is obvious now: we don't control the thing we are asserting it matches and we don't actually need it to match just a sufficient condition for it to probably be a good hash.

@satorg
Copy link
Contributor

satorg commented Feb 22, 2025

@johnynek ,

Also it would be easy to make a structural hash to cares about the construction order which we don't want.

Oh, I see now, that is a valid point. But in order to check it properly, I guess the test should look somewhat like this:

forAll { (x: Chain[Int]) =>
  assert(x.hashCode === Chain.fromIterableOnce(x.iterator).hashCode)
}

– that way we make sure Chain#hashCode doesn't depend on its structure, but also it doesn't depend on the List implementation either. Wdyt?

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

Successfully merging this pull request may close these issues.

Chain#hashCode is consistent with List#hashCode failure
5 participants