org.scalaequals

ScalaEquals

object ScalaEquals

Entry point for ScalaEquals

Examples: Test equality with only vals in constructor:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
  val w: Int = x * z      // Ignored by equal
  override def equals(other: Any) = ScalaEquals.equal
  override def hashCode() = ScalaEquals.hash
  def canEqual(other: Any) = ScalaEquals.canEquals
}

new Test(0, 1, 2, 3) == new Test(1, 1, 2, 4) // true -> y == y and z == z
new Test(0, 2, 2, 3) == new Test(1, 1, 2, 4) // false -> y != y and z == z

Test equality with vals in constructor AND body:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
  val w: Int = x * z
  def q: Int = x        // Ignored by equalAllVals
  override def equals(other: Any) = ScalaEquals.equalAllVals
  override def hashCode() = ScalaEquals.hash
  def canEqual(other: Any) = ScalaEquals.canEquals
}

new Test(0, 1, 2, 3) == new Test(1, 1, 2, 4) // false -> y == y and z == z and w != w
new Test(0, 2, 2, 3) == new Test(1, 1, 2, 4) // false -> y != y and z == z and w != w
new Test(1, 1, 2, 3) == new Test(1, 1, 2, 4) // true -> y == y and z == z and w == w

Test equality with selected parameters:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
  def w: Int = x * z
  override def equals(other: Any) = ScalaEquals.equal(w, a)
  override def hashCode() = ScalaEquals.hash
  def canEqual(other: Any) = ScalaEquals.canEquals
}

new Test(1, 2, 2, 3) == new Test(1, 1, 2, 4) // false -> w == w and a != a
new Test(1, 2, 2, 4) == new Test(1, 1, 2, 4) // true -> w == w and a == a

Specifically, the above example (test equality with selected parameters) is converted to the following code:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
  def w: Int = x * z
  override def equals(other: Any) = other match {
    case that: Test => (that canEqual this) && that.w == this.w && that.a == this.a
    case _ => false
  }
  override def hashCode() = MurmurHash3.seqHash(List(w, z))
  def canEqual(other: Any) = other.isInstanceOf[Test]
}

new Test(1, 2, 2, 3) == new Test(1, 1, 2, 4) // false -> w == w and a != a
new Test(1, 2, 2, 4) == new Test(1, 1, 2, 4) // true -> w == w and a == a

Generating toString:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
def w: Int = x * z
override def toString: String = ScalaEquals.genString
}

Which will expand to:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
def w: Int = x * z
override def toString: String = "Test(" + x + ", " + y + ", " + z + ", " + a + ")"
}

Generating toString with params:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
def w: Int = x * z
override def toString: String = ScalaEquals.genString(x, w)
}

Which will expand to:

class Test(x: Int, val y: Int, private val z: Int, var a: Int) {
def w: Int = x * z
override def toString: String = "Test(" + x + ", " + w + ")"
}
Version

1.2.0

Since

0.1.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. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def canEquals: Boolean

    Simple macro that expands to the following:

    Simple macro that expands to the following:

    other.isInstanceOf[Class]
    returns

    true if other.isInstanceOf[Class]

    Annotations
    @macroImpl()
  8. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  9. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  10. def equal: Boolean

    Equality check using all private/protected/public vals of the class defined in the constructor.

    Equality check using all private/protected/public vals of the class defined in the constructor. Does not use inherited or overriden vals. Do not use with traits, it will only generate a super.equals(that) call, instead use equalAllVals.

    returns

    true if instance.equals(other)

    Annotations
    @macroImpl()
  11. def equalAllVals: Boolean

    Equality check using all private/protected/public/lazy vals of the class defined in the constructor AND the body of the class.

    Equality check using all private/protected/public/lazy vals of the class defined in the constructor AND the body of the class. Does not use inherited or overriden vals.

    returns

    true if instance.equals(other)

    Annotations
    @macroImpl()
  12. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  13. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  14. def genString(param: Any, params: Any*): String

    Generates a string using only the passed in parameters

    Generates a string using only the passed in parameters

    Acceptable arguments include private/protected/public vals, vars, lazy vals, and defs with no arguments.

    param

    first param to test with

    params

    rest of the params

    returns

    "ClassName(param1, param2, ...)"

    Annotations
    @macroImpl()
  15. def genString: String

    Generates a string representation of the class using the constructor's arguments.

    Generates a string representation of the class using the constructor's arguments. Output is similar to case classes, for example, "ClassName(arg1, arg2, arg3)" would be the string returned for class ClassName(arg1: Int, arg2: Int, arg3: Int)

    returns

    string representation of calling class with constructor arguments

    Annotations
    @macroImpl()
  16. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  17. def hash: Int

    Looks up the elements tested in equals (including super.equals) and uses them in MurmurHash3.arrayHash(Array(elements)).

    Looks up the elements tested in equals (including super.equals) and uses them in MurmurHash3.arrayHash(Array(elements)). Works with all 3 forms of equal. Does not work with custom equals implementations, one of ScalaEquals.equal, ScalaEquals.equal(params), or ScalaEquals.equalAllVals must be used

    returns

    hashCode generated from fields used in equals

    Annotations
    @macroImpl()
  18. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  19. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  20. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  21. final def notify(): Unit

    Definition Classes
    AnyRef
  22. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  23. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  24. def toString(): String

    Definition Classes
    AnyRef → Any
  25. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  26. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  27. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()

Deprecated Value Members

  1. def equal(param: Any, params: Any*): Boolean

    Equality check using only parameters passed in to test for equality.

    Equality check using only parameters passed in to test for equality.

    Acceptable arguments include private/protected/public vals, vars, lazy vals, and defs with no arguments.

    param

    first param to test with

    params

    rest of the params

    returns

    true if instance.equals(other)

    Annotations
    @macroImpl() @deprecated
    Deprecated

    (Since version 1.2.0) Use ScalaEqualsExtend.equal(param, params...) instead. Will be removed in 1.3.0

Inherited from AnyRef

Inherited from Any

Ungrouped