Trait/Object

org.scalactic

TraversableEqualityConstraints

Related Docs: object TraversableEqualityConstraints | package scalactic

Permalink

trait TraversableEqualityConstraints extends SeqEqualityConstraints with SetEqualityConstraints with MapEqualityConstraints

Provides three implicit methods that loosen the equality constraint defined by TypeCheckedTripleEquals for Scala Traversables to one that more closely matches Scala's approach to Traversable equality.

Scala's approach to Traversable equality is that if the objects being compared are ether both Seqs, both Sets, or both Maps, the elements are compared to determine equality. This means you could compare an immutable Vector and a mutable ListBuffer for equality, for instance, and get true so long as the two Seqs contained the same elements in the same order. Here's an example:

scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> Vector(1, 2) == ListBuffer(1, 2)
res0: Boolean = true

Such a comparison would not, however, compile if you used === under TypeCheckedTripleEquals, because Vector and ListBuffer are not in a subtype/supertype relationship:

scala> import org.scalactic._
import org.scalactic._

scala> import TypeCheckedTripleEquals._
import TypeCheckedTripleEquals._

scala> Vector(1, 2) === ListBuffer(1, 2)
<console>:16: error: types scala.collection.immutable.Vector[Int] and
  scala.collection.mutable.ListBuffer[Int] do not adhere to the equality constraint selected for
  the === and !== operators; the missing implicit parameter is of type
  org.scalactic.CanEqual[scala.collection.immutable.Vector[Int],
  scala.collection.mutable.ListBuffer[Int]]
              Vector(1, 2) === ListBuffer(1, 2)
                           ^

If you mix or import the implicit conversion provided by TraversableEqualityConstraint, however, the comparison will be allowed:

scala> import TraversableEqualityConstraints._
import TraversableEqualityConstraints._

scala> Vector(1, 2) === ListBuffer(1, 2)
res2: Boolean = true

The equality constraints provided by this trait require that left and right sides are both subclasses of either scala.collection.GenSeq, scala.collection.GenSet, or scala.collection.GenMap, and that an CanEqual can be found for the element types for Seq and Set, or the key and value types for Maps. In the example above, both the Vector and ListBuffer are subclasses of scala.collection.GenSeq, and the regular TypeCheckedTripleEquals provides equality constraints for the element types, both of which are Int. By contrast, this trait would not allow a Vector[Int] to be compared against a ListBuffer[java.util.Date], because no equality constraint will exist between the element types Int and Date:

scala> import java.util.Date
import java.util.Date

scala> Vector(1, 2) === ListBuffer(new Date, new Date)
<console>:20: error: types scala.collection.immutable.Vector[Int] and
  scala.collection.mutable.ListBuffer[java.util.Date] do not adhere to the equality constraint selected for
  the === and !== operators; the missing implicit parameter is of type
  org.scalactic.CanEqual[scala.collection.immutable.Vector[Int],
  scala.collection.mutable.ListBuffer[java.util.Date]]
              Vector(1, 2) === ListBuffer(new Date, new Date)
                           ^

This trait simply mixes together SeqEqualityConstraints, SetEqualityConstraints, and MapEqualityConstraints.

Source
TraversableEqualityConstraints.scala
Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TraversableEqualityConstraints
  2. MapEqualityConstraints
  3. SetEqualityConstraints
  4. SeqEqualityConstraints
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

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

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

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

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

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

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int

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

    Permalink
    Definition Classes
    Any
  12. implicit def mapEqualityConstraint[KA, VA, CA[ka, kb] <: GenMap[ka, kb], KB, VB, CB[kb, vb] <: GenMap[kb, vb]](implicit equalityOfA: Equality[CA[KA, VA]], evKey: CanEqual[KA, KB], evValue: CanEqual[VA, VB]): CanEqual[CA[KA, VA], CB[KB, VB]]

    Permalink

    Provides an equality constraint that allows two subtypes of scala.collection.GenMaps to be compared for equality with === so long as an EqualityConstraint is available for both key types and both value types.

    Provides an equality constraint that allows two subtypes of scala.collection.GenMaps to be compared for equality with === so long as an EqualityConstraint is available for both key types and both value types.

    Definition Classes
    MapEqualityConstraints
  13. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  14. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  15. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. implicit def seqEqualityConstraint[EA, CA[ea] <: GenSeq[ea], EB, CB[eb] <: GenSeq[eb]](implicit equalityOfA: Equality[CA[EA]], ev: CanEqual[EA, EB]): CanEqual[CA[EA], CB[EB]]

    Permalink

    Provides an equality constraint that allows two subtypes of scala.collection.GenSeqs to be compared for equality with === so long as an EqualityConstraint is available for the element types.

    Provides an equality constraint that allows two subtypes of scala.collection.GenSeqs to be compared for equality with === so long as an EqualityConstraint is available for the element types.

    Definition Classes
    SeqEqualityConstraints
  17. implicit def setEqualityConstraint[EA, CA[ea] <: GenSet[ea], EB, CB[eb] <: GenSet[eb]](implicit equalityOfA: Equality[CA[EA]], ev: CanEqual[EA, EB]): CanEqual[CA[EA], CB[EB]]

    Permalink

    Provides an equality constraint that allows two subtypes of scala.collection.GenSets to be compared for equality with === so long as an EqualityConstraint is available for the element types.

    Provides an equality constraint that allows two subtypes of scala.collection.GenSets to be compared for equality with === so long as an EqualityConstraint is available for the element types.

    Definition Classes
    SetEqualityConstraints
  18. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  19. def toString(): String

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

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from MapEqualityConstraints

Inherited from SetEqualityConstraints

Inherited from SeqEqualityConstraints

Inherited from AnyRef

Inherited from Any

Ungrouped