|
Scalactic User Guide Custom equality Default equality Constrained equality Tolerance Normalization The Explicitly DSL Or and Every Requirements Snapshots TimesOnInt |
Constrained equality
Scalactic provides a way to get a compile-time type error if two types being compared with scala> import org.scalactic._ import org.scalactic._ scala> import TripleEquals._ import TripleEquals._ scala> Some(1) === 2 res0: Boolean = false scala> 1 === 1L res1: Boolean = true
scala> import TypeCheckedTripleEquals._
import TypeCheckedTripleEquals._
scala> Some(1) === 2
<console>:17: error: types Some[Int] and Int do not adhere to the type constraint selected for the === and !== operators;
the missing implicit parameter is of type org.scalactic.Constraint[Some[Int],Int]
Some(1) === 2
scala> 1 === 1L
<console>:17: error: types Int and Long do not adhere to the type constraint selected for the === and !== operators;
the missing implicit parameter is of type org.scalactic.Constraint[Int,Long]
1 === 1L
^
Note that the latter case is an example of an over-protective type error. If this expression were allowed to compile and run, it would yield
scala> 1 === (1L: AnyVal) res7: Boolean = true scala> 1L === (1: AnyVal) res8: Boolean = true
Since every type is a subtype of
Another way to address this type error, since an implicit conversion exists between the two types, is to use
scala> import ConversionCheckedTripleEquals._
import ConversionCheckedTripleEquals._
scala> Some(1) === 2
<console>:20: error: types Some[Int] and Int do not adhere to the type constraint selected for the === and !== operators;
the missing implicit parameter is of type org.scalactic.Constraint[Some[Int],Int]
Some(1) === 2
^
scala> 1 === 1L
res5: Boolean = true
The comparison between scala> 1L === 1 res6: Boolean = true
Another way to solve over-protective type errors is to actually supply
scala> List(1, 2, 3) === Vector(1, 2, 3)
<console>:14: error: types List[Int] and scala.collection.immutable.Vector[Int] do not adhere to the type constraint
selected for the === and !== operators; the missing implicit parameter is of type
org.scalactic.Constraint[List[Int],scala.collection.immutable.Vector[Int]]
List(1, 2, 3) === Vector(1, 2, 3)
^
Trait
scala> import TraversableEqualityConstraints._
import TraversableEqualityConstraints._
scala> List(1, 2, 3) === Vector(1, 2, 3)
res1: Boolean = true
scala> List(1, 2, 3) === Vector("1", "2", "3")
<console>:17: error: types List[Int] and scala.collection.immutable.Vector[String] do not adhere to the type constraint
selected for the === and !== operators; the missing implicit parameter is of type
org.scalactic.Constraint[List[Int],scala.collection.immutable.Vector[String]]
List(1, 2, 3) === Vector("1", "2", "3")
^
The last example doesn't compile because no equality constraint exists between the element types, Next, learn about Tolerance. |
Scalactic is brought to you by Bill Venners, with
contributions from several other folks. It is sponsored by
Artima, Inc.
ScalaTest is free, open-source software
released under the Apache
2.0 license.
Copyright © 2009-2025 Artima, Inc. All Rights Reserved.