mundacho
mundacho

Reputation: 239

Is eq always called in scala ==?

I have implemented a set wrapper using a canonical map (in scala). Now, when redefining the equals (and hashCode) I want the collections that contain my set wrapper to use reference equality, i.e. "eq". However, the collection that I use for the canonical map should use the real "equals". I've come up with the following solution:

  override def equals(obj: Any) =
  obj match {
    case o: SetWrapper => (o eq this) || o.set == this.set
    case _ => false
  }

My question is, do I really need this?

(o eq this) || o.set == this.set

or is it enough to use this?

  override def equals(obj: Any) =
  obj match {
    case o: SetWrapper => o.set == this.set
    case _ => false
  }

I'm guessing that the library automatically does the "eq" before calling the equals (when using ==), but I'm not sure.

Upvotes: 2

Views: 394

Answers (1)

Jean-Philippe Pellet
Jean-Philippe Pellet

Reputation: 59994

== calls equals, with a proper handling of null values. You have to call eq yourself if you want that optimization.

Upvotes: 2

Related Questions