All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalactic.TraversableEqualityConstraints.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2001-2013 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.scalactic

/**
 * 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. *

* * @author Bill Venners */ trait TraversableEqualityConstraints extends SeqEqualityConstraints with SetEqualityConstraints with MapEqualityConstraints /** * Companion object that facilitates the importing of TraversableEqualityConstraints members as * an alternative to mixing it in. One use case is to import TraversableEqualityConstraints members so you can use * them in the Scala interpreter. */ object TraversableEqualityConstraints extends TraversableEqualityConstraints




© 2015 - 2024 Weber Informatics LLC | Privacy Policy