org

scalaequals

package scalaequals

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 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) {
    override def equals(other: Any): Boolean = ScalaEquals.equal
    override def hashCode(): Int = ScalaEquals.hash
    def canEquals(other: Any): Boolean = ScalaEquals.canEquals
    override def toString: String = ScalaEquals.genString
}

// After macro expansion, the above is converted to:

class Point(val x: Int, val y: Int) {
     override def equals(other: Any): Boolean = other match {
       case that: Point => (that canEquals this) && that.x == this.x && that.y == this.y
       case _ => false
     }
     override def hashCode(): Int = MurmurHash3.seqHash(List(x, y))
     def canEquals(other: Any): Boolean = other.isInstanceOf[Point]
     override def toString: String = "Point(" + x + ", " + y + ")"
}

Things to note:

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)

Version

1.2.0

Since

0.3.0

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. scalaequals
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. object ScalaEquals

    Entry point for ScalaEquals

  2. object ScalaEqualsExtend

    Entry point for custom ScalaEquals macros.

  3. package impl

Inherited from AnyRef

Inherited from Any

Ungrouped