简体   繁体   中英

Combining Futures of different types using Cats

I have futures of different types.

import cats.Cartesian
import cats.instances.future._
import cats.syntax.cartesian._
import scala.concurrent.Future
import cats.implicits._

val aF : Future[Either[X, Y]] = getFuture(...)
val bF : Future[Either[X, Y]] = getFuture(...)
val cF = Future[List[Long]] = getFuture2(...)

val combinedFuture = Cartesian.tuple3(aF, bF, cF)
combinedFuture match {case (a, b, c) => 
   ...
}

But I get an error

Error:(36, 44) could not find implicit value for parameter cartesian: cats.Cartesian[scala.concurrent.Future]
      val combinedFuture = Cartesian.tuple3(aF, bF, cF)

But as you can see I have imported all the implicits, intances.future._ and syntax.

I am using Cats 0.9.0 on Scala 2.11.8

You're missing the implicit ExecutionContext :

import scala.concurrent.ExecutionContext.Implicits.global

I've had this happen many times when using the type class pattern on Future[T] , it is always the execution context which is easily forgettable, but makes the type class not resolve the implicits properly.

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