As my professor said, only the code tells the truth! So just take a look at the code that is created for:
case class A(i: Int, s: String)
We can instruct the Scala compiler to show us the generated code after different phases, here after typechecker:
% scalac -Xprint:typer test.scala [[syntax trees at end of typer]]// Scala source: test.scala package <empty> { @serializable case class A extends java.lang.Object with ScalaObject with Product { .. override def hashCode(): Int = ScalaRunTime.this._hashCode(A.this); ... override def equals(x$1: Any): Boolean = A.this.eq(x$1).||(x$1 match { case (i: Int,s: String)A((i$1 @ _), (s$1 @ _)) if i$1.==(i).&&(s$1.==(s)) => x$1.asInstanceOf[A].canEqual(A.this) case _ => false }); override def canEqual(x$1: Any): Boolean = x$1.$isInstanceOf[A]() }; }
So you can see that the calculation of the hash code has been delegated to ScalaRunTime._hashCode , and equality depends on the equality of the case 'class.
Mirko Stocker May 03 '11 at 8:35 a.m. 2011-03-03 08:35
source share