score:13

Accepted answer

Not sure about the value of this, but you can inline the implementation of ScalaRuntime._hashCode:

case class Foo(s: String) {
  override val hashCode: Int = scala.util.hashing.MurmurHash3.productHash(this)
}

score:0

toString of the case class instance is solely dependent on its fields not hashCode

score:1

@Rumoku's answer seems to get at what's really going on here. When you declare case class Foo it compiles into this: case class Foo extends AnyRef with Product with Serializable (see scalac's -Xprint:typer option). In addition, an implementation of hashCode is also generated as follows: override def hashCode: Int = scala.runtime.ScalaRunTime._hashCode(this). When you override hashCode yourself however, this implementation is not generated. The key is that neither Product nor Serializable implement hashCode so the implementation that you're picking up when you call super.hashCode is the default implementation from AnyRef/Any/Object.

score:4

Not sure what you mean by "cached hasCode", but ... You've override hashCode with custom solution that built from Object, that is why you're getting false. Remove this override and you'll get expected value.


Related Query

More Query from same tag