【问题标题】:Scala Cats Error when Implementing Eq Type Class实现 Eq 类型类时的 Scala Cats 错误
【发布时间】:2018-07-20 09:36:20
【问题描述】:

我正在尝试为案例类实现 Eq,但我不断收到编译器错误。我很确定我错过了一些进口,但不确定是哪一个。这是我所做的:

import cats.Eq
import cats.syntax.eq._

final case class Cat(name: String, age: Int, color: String)
implicit val catEq: Eq[Cat] = Eq.instance[Cat] { (cat1, cat2) =>
  cat1.name === cat2.name && cat1.age === cat2.age && cat1.color === cat2.color 
}

我正在使用cats-core 1.0.1 库。以下是编译器所说的:

<console>:17: error: value === is not a member of String
         cat1.name === cat2.name && cat1.age === cat2.age && cat1.color === cat2.color
                   ^
<console>:17: error: value === is not a member of Int
         cat1.name === cat2.name && cat1.age === cat2.age && cat1.color === cat2.color
                                             ^
<console>:17: error: value === is not a member of String
         cat1.name === cat2.name && cat1.age === cat2.age && cat1.color === cat2.color

【问题讨论】:

    标签: scala scala-cats


    【解决方案1】:

    对于String,您缺少Eq 的实例

    你可以用

    import cats.instances.string._
    

    【讨论】:

    • 我太愚蠢了,竟然错过了那个导入!现在知道了,感谢您指出!
    猜你喜欢
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 2018-12-16
    • 2018-01-04
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多