missing Cats Functor[Future] instance

10,212

Solution 1

By importing cats.implicits._ you are actually already importing cats.syntax.AllSyntax and cats.instances.AllInstances

Try using just those imports:

import cats.data._
import cats.implicits._

or (according to your needs):

import cats.data._
import cats.instances.future._

or more specifically:

import cats.data._
import cats.instances.future.catsStdInstancesForFuture

you may also need:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

Note: of course you have to implicitly provide an actual ExecutionContext in a production environment.

Solution 2

Following imports work for me (also mentioned in approved answer),

import cats.data.OptionT
import cats.instances.future._ // or import cats.implicits._ 
                               // as implicits include FutureInstances

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

Also, the important thing was the dependencies as I was using org.typelevel:cats:0.9.0 along with cats-core-1.1.0 which was causing Symbol 'type cats.kernel.instances.EqInstances' is missing from the classpath.

Had to remove older cats-0.9.0 and use latest cats-core and cats-kernel.

libraryDependencies ++= Seq(
  "org.typelevel" %% "cats-core" % "1.1.0",
  "org.typelevel" %% "cats-kernel" % "1.2.0",

  "org.scalatest" %% "scalatest" % "3.0.4" % Test
)

Solution 3

Seems like causes of this issue can be various; I just ran into this because I had two implicit ExecutionContexts available in scope, so cats was unable to select one to provide the Functor[Future]:

import scala.concurrent.ExecutionContext.Implicits.global
// ^ first implicit ExecutionContext

class MyClass {
  def myMethod(e: EitherT[Future, _, _])(implicit ec: ExecutionContext) {
    // Second implicit ExecutionContext
    e.flatMap(...) // <- Functor unavailable
  }
}

In my case I was able to resolve the issue by simply not passing ec into myMethod, though removing the global execution context would have also worked.

Share:
10,212

Related videos on Youtube

kostja
Author by

kostja

We are here to learn :) Works and plays with scala, kafka, data, linux

Updated on October 21, 2022

Comments

  • kostja
    kostja over 1 year

    I am trying to use OptionT to combine methods returning Future[Option[T]] in a for-comprehension.

    import cats.data._
    import cats.implicits._
    import cats.instances.future._
    
    for {
      data <- OptionT(repo.getData(id))
      ... 
    }
    

    The compiler error I am getting:

    could not find implicit value for parameter F cats.Functor[scala.concurrent.Future]
    

    This recent example suggests that this is (was?) possible.

    so do the docs in the pull request for adding OptionT

    and the cats Functor docs

    What am I missing here?

    Thank you

  • kostja
    kostja about 7 years
    removing import cats.instances.future._ and adding the ExecutionContext fixed this. Thank you, Federico.
  • Freewind
    Freewind about 6 years
    I use import cats.instances.future.catsStdInstancesForFuture only, and remove all other related cats imports, can fix this error too