Trait/Object

org.scalactic

SetEqualityConstraints

Related Docs: object SetEqualityConstraints | package scalactic

Permalink

trait SetEqualityConstraints extends AnyRef

Provides an implicit method that loosens the equality constraint defined by TypeCheckedTripleEquals or ConversionCheckedTripleEquals for Scala Sets to one that more closely matches Scala's approach to Set equality.

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

scala> import scala.collection.immutable.TreeSet
import scala.collection.immutable.TreeSet

scala> import scala.collection.mutable.HashSet
import scala.collection.mutable.HashSet

scala> TreeSet(1, 2) == HashSet(1, 2)
res0: Boolean = true

Such a comparison would not, however, compile if you used === under either TypeCheckedTripleEquals or ConversionCheckedTripleEquals, because TreeSet and HashSet are not in a subtype/supertype relationship, nor does an implicit conversion by default exist between them:

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

scala> import TypeCheckedTripleEquals._
import TypeCheckedTripleEquals._

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

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

scala> import SetEqualityConstraints._
import SetEqualityConstraints._

scala> TreeSet(1, 2) === HashSet(1, 2)
res2: Boolean = true

The equality constraint provided by this trait requires that both left and right sides are subclasses of scala.collection.GenSet and that an EqualityConstraint can be found for the element types. In the example above, both the TreeSet and HashSet are subclasses of scala.collection.GenSet, and the regular TypeCheckedTripleEquals provides equality constraints for the element types, both of which are Int. By contrast, this trait would not allow a TreeSet[Int] to be compared against a HashSet[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> TreeSet(1, 2) === HashSet(new Date, new Date)
<console>:20: error: types scala.collection.immutable.TreeSet[Int] and
  scala.collection.mutable.HashSet[java.util.Date] do not adhere to the equality constraint selected for
  the === and !== operators; the missing implicit parameter is of type
  org.scalactic.EqualityConstraint[scala.collection.immutable.TreeSet[Int],
  scala.collection.mutable.HashSet[java.util.Date]]
              TreeSet(1, 2) === HashSet(new Date, new Date)
                            ^

Source
SetEqualityConstraints.scala
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. SetEqualityConstraints
  2. AnyRef
  3. 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. final def ne(arg0: AnyRef): Boolean

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

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

    Permalink
    Definition Classes
    AnyRef
  15. 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.

  16. final def synchronized[T0](arg0: ⇒ T0): T0

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

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

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

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

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

Inherited from AnyRef

Inherited from Any

Ungrouped