简体   繁体   中英

missing Cats Functor[Future] instance

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

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.

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
)

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

Seems like causes of this issue can be various; I just ran into this because I had two implicit ExecutionContext s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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