I want to define equality for some type that can be part of other objects or collections using cats/kitten. I don't want to have to define the equality for every other class. For example:
import cats.Eq
case class Foo(a: Int, bar: Bar)
case class Bar(b: Int)
object BarEq {
implicit val barEq: Eq[Bar] = Eq.instance[Bar] {
(b1, b2) => b1.b +1 == b2.b
}
}
and then a test defined as
import cats.derived.auto.eq._
class BarEqTest extends FlatSpec with cats.tests.StrictCatsEquality {
import BarEq._
"bareq" should {
"work" in {
Foo(1, Bar(1)) should ===(Foo(1, Bar(2)))
Bar(1) should ===(Bar(2))
Some(Foo(1, Bar(1))) should ===(Some(Foo(1, Bar(2))))
}
}
}
this works fine, but if I try to add the following test case
Seq(Foo(1, Bar(1))) should ===(Seq(Foo(1, Bar(2))))
I get
[Error] types Seq[Foo] and Seq[Foo] do not adhere to the type constraint selected for the === and !== operators; the missing implicit parameter is of type org.scalactic.CanEqual[Seq[Foo],Seq[Foo]]
one error found
How come the auto derived eq works with Option
but not Seq
, and how can I make it work?
I tried to add import cats.instances.seq._
but that did not work either.
Cats defines an instance of
Eq
forscala.collection.immutable.Seq
, not for genericscala.collection.Seq
(or moreoverscala.collection.mutable.Seq
)https://github.com/typelevel/cats/blob/main/kernel/src/main/scala/cats/kernel/Eq.scala#L283-L285
Starting from Scala 2.13.0
scala.Seq
isscala.collection.immutable.Seq
. But in Scala 2.12.xscala.Seq
isscala.collection.Seq
.https://github.com/scala/scala/releases/tag/v2.13.0
So in Scala 2.12 import correct collection type
or define your own instance of
Eq
for necessary collections