ScalaEquals provides easy to use macros for generating correct equals/hashCode/canEquals
implementations, never look up an equals/hashCode recipe again!
The methods generated from ScalaEquals are taken directly from Programming in Scala*
and strictly obey the contract of equals:
Reflexive
Symmetric
Transitive
As well as the additional properties that equals is consistent (x.equals(y) consistently
returns one of true or false) and that for any non-null value x, x.equals(null) always
returns false. Additionally, using ScalaEquals.hash will guarantee that hashCode()
is always consistent with equals. In the documentation, anywhere ScalaEquals.equal
is seen it is assumed that ScalaEquals.equalAllVals also applies, unless otherwise
stated. Additionally, in the documentation the argument names are assumed to be other,
but this is not a requirement, you may name the parameters however you would like.
The typical use case is as follows:
class Point(val x: Int, val y: Int) {
overridedef equals(other: Any): Boolean = ScalaEquals.equal
overridedef hashCode(): Int = ScalaEquals.hash
def canEquals(other: Any): Boolean = ScalaEquals.canEquals
overridedef toString: String = ScalaEquals.genString
}
// After macro expansion, the above is converted to:class Point(val x: Int, val y: Int) {
overridedef equals(other: Any): Boolean = other match {
case that: Point => (that canEquals this) && that.x == this.x && that.y == this.y
case _ =>false
}
overridedef hashCode(): Int = MurmurHash3.seqHash(List(x, y))
def canEquals(other: Any): Boolean = other.isInstanceOf[Point]
overridedef toString: String = "Point(" + x + ", " + y + ")"
}
Things to note:
The macros will intelligently pick other methods to call. Specifically, if the class extends
a class that also override equals, super.equals(that) will be called. If canEquals is not
defined, it will not be added to the equals call. ScalaEquals.hash will call super.hashCode()
if and only if super.equals(that) is called.
The macros may only be called from their respectively named methods, ScalaEquals.equal can
only be called in a method named equals. ScalaEquals.hash can only be called in a method named
hashCode(). ScalaEquals.canEquals can only be called in a method name canEquals. This serves
as a safety net to ensure these methods are never called somewhere they are not supposed to be.
* Programming in Scala by Martin Odersky, Lex Spoon and Bill Venners,
Artima Press, 2008: Chapter 28 (Object equality) / Second Edition, 2011:
Chapter 30 (Object equality)
ScalaEquals
ScalaEquals provides easy to use macros for generating correct equals/hashCode/canEquals implementations, never look up an equals/hashCode recipe again! The methods generated from
ScalaEquals
are taken directly from Programming in Scala* and strictly obey the contract of equals:As well as the additional properties that equals is consistent (
x.equals(y)
consistently returns one of true or false) and that for any non-null valuex
,x.equals(null)
always returns false. Additionally, usingScalaEquals.hash
will guarantee thathashCode()
is always consistent withequals
. In the documentation, anywhereScalaEquals.equal
is seen it is assumed thatScalaEquals.equalAllVals
also applies, unless otherwise stated. Additionally, in the documentation the argument names are assumed to beother
, but this is not a requirement, you may name the parameters however you would like.The typical use case is as follows:
Things to note:
super.equals(that)
will be called. IfcanEquals
is not defined, it will not be added to theequals
call.ScalaEquals.hash
will callsuper.hashCode()
if and only ifsuper.equals(that)
is called.ScalaEquals.equal
can only be called in a method namedequals
.ScalaEquals.hash
can only be called in a method namedhashCode()
.ScalaEquals.canEquals
can only be called in a method namecanEquals
. This serves as a safety net to ensure these methods are never called somewhere they are not supposed to be.See the github readme for more information. Also, see org.scalaequals.ScalaEquals for additional examples.
* Programming in Scala by Martin Odersky, Lex Spoon and Bill Venners, Artima Press, 2008: Chapter 28 (Object equality) / Second Edition, 2011: Chapter 30 (Object equality)
1.2.0
0.3.0