Search code examples
scalascala-cats

Scala Cats Error when Implementing Eq Type Class


I'm trying to implement a Eq for a case class and I keep getting compiler errors. I'm pretty sure that I'm missing some imports, but not sure which one. Here is what I did:

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 
}

I'm using the cats-core 1.0.1 library. Here is what the compiler says:

<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

Solution

  • You're missing the instance of Eq for String

    You can import it with

    import cats.instances.string._