make hash.fromUniversalHashCode null safe#4860
Open
mattjliu wants to merge 3 commits into
Open
Conversation
satorg
reviewed
May 9, 2026
Comment on lines
+38
to
+50
| test("hash extension method is consistent with hashCode for Int") { | ||
| assert(1.hash == 1.hashCode) | ||
| } | ||
|
|
||
| test("hash extension method is consistent with hashCode for String") { | ||
| assert("ABC".hash == "ABC".hashCode) | ||
| } | ||
|
|
||
| test("fromUniversalHashCode should be consistent with hashCode on non-null references") { | ||
| case class ExplicitHashCode() { override val hashCode = 42 } | ||
| val hash = Hash.fromUniversalHashCode[ExplicitHashCode] | ||
| assertEquals(hash.hash(ExplicitHashCode()), 42) | ||
| } |
Contributor
There was a problem hiding this comment.
These 3 tests would work better as properties, e.g.:
property("hash extension method is consistent with hashCode for Int") {
Prop.forAll { (n: Int) =>
assertEqual(n.hash, n.hashCode)
}
}
Contributor
There was a problem hiding this comment.
case class ExplicitHashCode() { override val hashCode = 42 }
Out of curiosity: is case class used instead of class in order to avoid new in ExplicitHashCode() two lines later? Doesn't seem saving a lot, but the compiler will generate more stuff for case class comparing to a regular one.
Author
There was a problem hiding this comment.
makes sense , It's a case class because I copied the test format from #4790, which used a case class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #4831