繁体   English   中英

实现Eq类型时Scala Cats错误

[英]Scala Cats Error when Implementing Eq Type Class

我正在尝试为一个案例类实现一个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

你错过了Eq for String的实例

你可以用它导入它

import cats.instances.string._

暂无
暂无

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

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