Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missing Cats Functor[Future] instance

Tags:

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

like image 769
kostja Avatar asked Apr 13 '17 09:04

kostja


2 Answers

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 ) 
like image 43
prayagupa Avatar answered Sep 17 '22 14:09

prayagupa


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.

like image 193
Federico Pellegatta Avatar answered Sep 19 '22 14:09

Federico Pellegatta