Case class hashing

Currently the hashing of case classes are delegated to scala.util.hashing.MurmurHash3.productHash.
This function takes into account the productPrefix only when the productArity is 0.
This produces the following problem:

scala> Left(1).##
res1: Int = 818387364

scala> Right(1).##
res2: Int = 818387364

Should productPrefix always be mixed into the hashCode by using productPrefix.## as the seed?

1 Like

Yes, that looks like a bug. Dotty-compiled case classes do take Product prefix into account.

scala> case class Left(x: Int) 
defined class Left
scala> case class Right(x: Int) 
defined class Right
scala> Left(1).## 
val res3: Int = -1674650338
scala> Right(1).## 
val res4: Int = 1333177388

Thanks Martin. I’ll start an issue on scala/bug. This relates to a Hash typeclass I’m working on for cats.

1 Like