org.scalautils

Equality

trait Equality [A] extends AnyRef

Defines a custom way to determine equality for a type.

For example, here's how you could define equality between Doubles such that a tolerance of ± 0.01 is allowed:

class TolerantDoubleEquality extends Equality[Double] {

  private val Tol = 0.01

  def areEqual(a: Double, b: Any): Boolean = {
    b match {
      case bDouble: Double => (a <= bDouble + Tol) && (a >= bDouble - Tol)
      case _ => false
    }
  }
}

If an implicit instance of TolerantDoubleEquality is in scope, it will be used by ScalaTest's === operators and its should equal and should === matcher syntax. Here's an example:

$ scala -cp target/jar_contents/
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import org.scalautils._
import org.scalautils._

scala> class TolerantDoubleEquality extends Equality[Double] {
     |
     |   private val Tol = 0.01
     |
     |   def areEqual(a: Double, b: Any): Boolean = {
     |     b match {
     |       case bDouble: Double => (a >= bDouble + Tol) && (a >= bDouble - Tol)
     |       case _ => false
     |     }
     |   }
     | }
defined class TolerantDoubleEquality

scala> import TripleEquals._
import TripleEquals._

scala> 2.0 === 2.001
res0: Boolean = false

scala> implicit val tolerantDoubleEquality = new TolerantDoubleEquality
tolerantDoubleEquality: TolerantDoubleEquality = TolerantDoubleEquality@70c13c17

scala> 2.0 === 2.001
res1: Boolean = true

Note: The Equality type class was inspired in part by the Equal type class of the scalaz project.

A

the type whose equality is being customized

Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. Equality
  2. AnyRef
  3. Any
Visibility
  1. Public
  2. All

Abstract Value Members

  1. def areEqual (a: A, b: Any): Boolean

    Indicates whether the objects passed as a and b are equal.

    Indicates whether the objects passed as a and b are equal.

    a

    a left-hand-side object being compared with another (right-hand-side one) for equality (e.g., a == b)

    b

    a right-hand-side object being compared with another (left-hand-side one) for equality (e.g., a == b)

    returns

    true if the passed objects are "equal," as defined by this Equality instance

    Attributes
    abstract

Concrete Value Members

  1. def != (arg0: AnyRef): Boolean

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

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

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

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

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

    Attributes
    final
    Definition Classes
    Any
  7. def clone (): AnyRef

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

    Attributes
    final
    Definition Classes
    AnyRef
  9. def equals (arg0: Any): Boolean

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

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  11. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef → Any
  12. def hashCode (): Int

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

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

    Attributes
    final
    Definition Classes
    AnyRef
  15. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  16. def notifyAll (): Unit

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

    Attributes
    final
    Definition Classes
    AnyRef
  18. def toString (): String

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

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

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

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any