monnef
monnef

Reputation: 4053

How to use cats typeclasses?

import cats._
import cats.data._
import cats.implicits._
import cats.instances.all._

...
  def test = Foldable[List].intercalate(List("a", "b", "c"), "-")
...

results into:

could not find implicit value for parameter instance: cats.Foldable[List]

Why it isn't working and how to fix it? It seems like it is just a copy from their docs:

scala> import cats.implicits._
scala> Foldable[List].intercalate(List("a","b","c"), "-")
res0: String = a-b-c

Edit 1:

Even after removing import cats.instances.all._ as suggested it still doesn't work. Now it crashes with:

value intercalate is not a member of cats.Foldable[List]

Upvotes: 1

Views: 227

Answers (1)

Jasper-M
Jasper-M

Reputation: 15086

You are importing the instance for Foldable[List] twice: once through import cats.implicits._ and once through import cats.instances.all._. They become ambiguous that way.

Looking at the source, you can see that cats.instances.all is a subset of cats.implicits. Simplified:

package cats {
  package object instances {
    object all extends AllInstances
  }

  object implicits extends syntax.AllSyntax with instances.AllInstances
}

Also, it seems Foldable.intercalate wasn't added until cats 0.9.0.

Upvotes: 2

Related Questions