Skip to content

make hash.fromUniversalHashCode null safe#4860

Open
mattjliu wants to merge 3 commits into
typelevel:mainfrom
mattjliu:null-safe-hash
Open

make hash.fromUniversalHashCode null safe#4860
mattjliu wants to merge 3 commits into
typelevel:mainfrom
mattjliu:null-safe-hash

Conversation

@mattjliu
Copy link
Copy Markdown

@mattjliu mattjliu commented May 8, 2026

fixes #4831

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)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)
  }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

makes sense , It's a case class because I copied the test format from #4790, which used a case class

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.

Make Hash.fromUniversalHashCode null safe

2 participants