【问题标题】:Combining Futures of different types using Cats使用 Cats 组合不同类型的 Future
【发布时间】:2017-07-13 21:14:18
【问题描述】:

我有不同类型的未来。

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) => 
   ...
}

但我得到一个错误

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

但正如你所见,我已经导入了所有的隐式、intances.future._ 和语法。

我在 Scala 2.11.8 上使用 Cats 0.9.0

【问题讨论】:

  • 实际上,导入 cats.implicits._ 和单独导入 cats.instances.future._ 会产生冲突的隐含。不要同时使用。

标签: scala scala-cats


【解决方案1】:

你错过了隐含的ExecutionContext

import scala.concurrent.ExecutionContext.Implicits.global

Future[T] 上使用类型类模式时,我已经多次发生这种情况,它总是很容易忘记的执行上下文,但会导致类型类无法正确解析隐式。

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 2012-06-24
    • 2020-02-24
    • 1970-01-01
    • 2020-09-24
    • 2020-09-11
    • 2017-09-09
    • 1970-01-01
    • 2020-10-13
    相关资源
    最近更新 更多