The Artima Developer Community
Sponsored Link

Programming in Scala Forum
canEqual improvements

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Blair Zajac

Posts: 4
Nickname: blairzajac
Registered: Jan, 2008

canEqual improvements Posted: Jan 14, 2010 9:48 PM
Reply to this message Reply
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.

Topic: scala db programming tutorial Previous Topic   Next Topic Topic: Buy PDF-only, upgrade to hardcopy later?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use