我正在尝试使用Scalaz EitherT和scala.concurrent.Future.当试图在for-comprehension中使用它时:
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
将为您提供全局执行上下文.
完整示例:
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")