Blair Zajac
Posts: 4
Nickname: blairzajac
Registered: Jan, 2008
|
|
canEqual improvements
|
Posted: Jan 14, 2010 9:48 PM
|
|
Advertisement
|
I was implementing the canEqual and noticed a few small improvements one can made.
For example, the book recommends:
class Foo(val i: Int)
{
override
def equals(other: Any): Boolean =
{
other match {
case that: Foo => that.canEqual(this) && this.i == that.i
case _ => false
}
}
def canEqual(other: Any): Boolean =
{
other.isInstanceOf[Foo]
}
}
The minor performance and code cleaniness is that you can make canEquals() protected and it can take the base class and return true for the base one:.
protected def canEqual(other: Foo) = true
Of course, any subclasses should define canEqual() correctly, but there's no point in doing an isInstanceOf() in the most base version.
|
|