missing Cats Functor[Future] instance
Asked Answered
E

4

25

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

Encircle answered 13/4, 2017 at 9:29 Comment(0)
E
39

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.

Ebenezer answered 13/4, 2017 at 9:48 Comment(2)
removing import cats.instances.future._ and adding the ExecutionContext fixed this. Thank you, Federico.Encircle
I use import cats.instances.future.catsStdInstancesForFuture only, and remove all other related cats imports, can fix this error tooAmaro
B
1

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
)
Biancabiancha answered 22/8, 2018 at 19:32 Comment(0)
S
1

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.

Scots answered 7/7, 2021 at 17:31 Comment(0)
P
0

Had the same problem. cats actually has the instance.

The real issue is just the implicit ExecutionContext missing which you could just do the following

import scala.concurrent.ExecutionContext.Implicits.global

I realised this when I was trying to provide an instance for it.

    implicit val ev: Functor[Future] = new Functor[Future] {
      override def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa.map(f)
    }

It's at this point the compiler will say the missing the implicit ExecutionContext

Praefect answered 3/11, 2019 at 0:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.