org.scalactic

MapEqualityConstraints

trait MapEqualityConstraints extends AnyRef

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

Scala's approach to Map equality is that if both objects being compared are Maps, the elements are compared to determine equality. This means you could compare an immutable TreeMap and a mutable HashMap for equality, for instance, and get true so long as the two maps contained the same key-value mappings. Here's an example:

scala> import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeMap

scala> import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap

scala> TreeMap("one" -> 1, "two" -> 2) == HashMap("one" -> 1, "two" -> 2)
res0: Boolean = true

Such a comparison would not, however, compile if you used === under either TypeCheckedTripleEquals or ConversionCheckedTripleEquals, because TreeMap and HashMap 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> TreeMap("one" -> 1, "two" -> 2) === HashMap("one" -> 1, "two" -> 2)
<console>:16: error: types scala.collection.immutable.TreeMap[String,Int] and
  scala.collection.mutable.HashMap[String,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.TreeMap[String,Int],
  scala.collection.mutable.HashMap[String,Int]]
              TreeMap("one" -> 1, "two" -> 2) === HashMap("one" -> 1, "two" -> 2)
                                              ^

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

scala> import MapEqualityConstraints._
import MapEqualityConstraints._

scala> TreeMap("one" -> 1, "two" -> 2) === HashMap("one" -> 1, "two" -> 2)
res2: Boolean = true

The equality constraint provided by this trait requires that both left and right sides are subclasses of scala.collection.GenMap and that an EqualityConstraint can be found for both key types and both value types. In the example above, both the TreeMap and HashMap are subclasses of scala.collection.GenMap, and the regular TypeCheckedTripleEquals provides equality constraints for the key types, both of which are String, and value types, both of which are Int. By contrast, this trait would not allow a TreeMap[String, Int] to be compared against a HashMap[String, java.util.Date], because no equality constraint will exist between the value types Int and Date:

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

scala> TreeMap("one" -> 1, "two" -> 2) === HashMap("one" -> new Date, "two" -> new Date)
<console>:20: error: types scala.collection.immutable.TreeMap[String,Int] and
  scala.collection.mutable.HashMap[String,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.TreeMap[String,Int],
  scala.collection.mutable.HashMap[String,java.util.Date]]
              TreeMap("one" -> 1, "two" -> 2) === HashMap("one" -> new Date, "two" -> new Date)
                                              ^

Source
MapEqualityConstraints.scala
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. MapEqualityConstraints
  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: Any): Boolean

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

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

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

    Definition Classes
    Any
  5. def clone(): AnyRef

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

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

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

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

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

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

    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: Constraint[KA, KB], evValue: Constraint[VA, VB]): Constraint[CA[KA, VA], CB[KB, VB]]

    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.

  13. final def ne(arg0: AnyRef): Boolean

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

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

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

    Definition Classes
    AnyRef
  17. def toString(): String

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

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped