Scala scalaz.Monad [scala.concurrent.Future],执行上下文怎么样?

时间:2014-11-09 21:39:00

标签: scala monads future scalaz

我看到一些问题利用scalaz Monad看起来像scala Future。 Herehere。我还没有看到一种令人满意的方法,可以在不使用全局执行上下文的情况下将其解析为隐式类型,但我觉得这些类型类的导入不应该具有上下文的静态知识。 这里有什么我想念的吗? (我假设他们没有使用scalaz.concurrent.Future)

1 个答案:

答案 0 :(得分:1)

只需要在您的Monad被称为Future的呼叫站点隐式提供ExecutionContext。我同意程序中存在的类型类可能有多种不同的定义存在一些尴尬,但是没有必要静态地依赖它的实现。

import scala.concurrent.Future
import scalaz._
import Scalaz._

def foo[A, T[_]: Traverse, M[_]: Monad](t: T[M[A]]): M[T[A]] =
  implicitly[Traverse[T]].sequence(t)

def bar(l: List[Future[Int]])(implicit ctx: ExecutionContext): Future[List[Int]] = 
  foo(l)

https://github.com/scalaz/scalaz/blob/v7.1.0/core/src/main/scala/scalaz/std/Future.scala#L8