From the source code scala/Equals.scala
(here):
package scala
trait Equals extends scala.Any {
def canEqual(that: scala.Any): scala.Boolean
def equals(that: scala.Any): scala.Boolean
}
In the documentation, it says:
A method that should be called from every well-designed equals method that is open to be overridden in a subclass.
I randomly picked a class which extends scala.Equals
and which is simple enough to understand. I picked scala.Tuple2[+T1, +T2]
, which extends the trait scala.Product[T1, T2]
, which in turn extends the trait scala.Product
, which in turn extends the trait scala.Equals
.
Unfortunately, it seems that because scala.Tuple2
is a case class, the canEqual()
and equals()
methods are automatically generated and therefore could not be found in the source code scala/Tuple2.scala
(here).
My questions are:
- When is it a good time to extend the trait
scala.Equals
? - How should
canEqual()
be implemented? - What are the best practices (or boilerplate) to use
canEqual()
inequals()
?
Thanks in advance!
PS: In case if it matters, I'm using Scala 2.11.7.