简体   繁体   English

使用 Seq 自动衍生的 Cats

[英]Cats auto derived with Seq

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?为什么自动派生的 eq 可以与Option但不能与Seq ,我该如何使其工作? I tried to add import cats.instances.seq._ but that did not work either.我试图添加import cats.instances.seq._但这也不起作用。

Cats defines an instance of Eq for scala.collection.immutable.Seq , not for generic scala.collection.Seq (or moreover scala.collection.mutable.Seq ) Cats 为scala.collection.immutable.Seq定义了一个Eq实例,而不是为泛型scala.collection.Seq (或者scala.collection.mutable.Seq

import scala.collection.immutable.{BitSet, Queue, Seq, SortedMap, SortedSet}

private[kernel] trait EqInstances0 {
  implicit def catsKernelEqForSeq[A: Eq]: Eq[Seq[A]] = cats.kernel.instances.seq.catsKernelStdEqForSeq[A]
}

https://github.com/typelevel/cats/blob/main/kernel/src/main/scala/cats/kernel/Eq.scala#L283-L285 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 is scala.collection.immutable.Seq .从 Scala 2.13.0 scala.Seqscala.collection.immutable.Seq But in Scala 2.12.x scala.Seq is scala.collection.Seq .但是在 Scala 2.12.x 中, scala.Seqscala.collection.Seq

https://github.com/scala/scala/releases/tag/v2.13.0 https://github.com/scala/scala/releases/tag/v2.13.0

So in Scala 2.12 import correct collection type所以在 Scala 2.12 中导入正确的集合类型

import scala.collection.immutable.Seq
Seq(Foo(1, Bar(1))) === Seq(Foo(1, Bar(2))) // true

or define your own instance of Eq for necessary collections或为必要的集合定义您自己的Eq实例

import cats.kernel.instances.StaticMethods

implicit def genericSeqEq[A: Eq]: Eq[collection.Seq[A]] = new Eq[collection.Seq[A]] {
  override def eqv(xs: collection.Seq[A], ys: collection.Seq[A]): Boolean =
    if (xs eq ys) true
    else StaticMethods.iteratorEq(xs.iterator, ys.iterator)
}

Seq(Foo(1, Bar(1))) === Seq(Foo(1, Bar(2))) // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM