我正在尝试将 Scalaz EachT 与 scala.concurrent.Future 一起使用。当试图在理解中使用它时:
import scalaz._
import Scalaz._
val et1:EitherT[Future, String, Int] = EitherT(Future.successful(1.right))
val et2:EitherT[Future, String, String] = EitherT(Future.successful("done".right))
val r:EitherT[Future, String, String] = for {
a <- et1
b <- et2
} yield (s"$a $b")
我收到以下丢失的 Functor 和 Monad 实例错误:
could not find implicit value for parameter F: scalaz.Functor[scala.concurrent.Future]
b <- et2
^
could not find implicit value for parameter F: scalaz.Monad[scala.concurrent.Future]
a <- et1
scalaz 是否为 Functor 和 Monad for Future 定义实例?如果没有,是否还有其他库提供这些实例,或者我是否需要编写它们?
最佳答案
你需要一个隐含的 ExecutionContext
在适用范围。 import ExecutionContext.Implicits.global
将为您提供 global execution context .
完整示例:
import scala.concurrent.ExecutionContext.Implicits.global
import scalaz._
import Scalaz._
val et1:EitherT[Future, String, Int] = EitherT(Future.successful(1.right))
val et2:EitherT[Future, String, String] = EitherT(Future.successful("done".right))
val r:EitherT[Future, String, String] = for {
a <- et1
b <- et2
} yield s"$a $b"
val foo = Await.result(r.run, 1 seconds)
// => \/-("1 done")
关于scala - 使用带有任一 T 的 scala.concurrent.Future 时缺少 Functor 和 Monad 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34407163/