Monday, 9 September 2013

Is eq always called in scala ==?

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.

No comments:

Post a Comment