【问题标题】:Scala scalaz.Monad[scala.concurrent.Future], what about the execution context?Scala scalaz.Monad[scala.concurrent.Future],执行上下文呢?
【发布时间】:2015-01-06 03:45:41
【问题描述】:

我看到了几个使用 scalaz Monad 来实现看起来像 scala Future 的问题。 Herehere。在不使用全局执行上下文的情况下,我还没有看到将其作为隐式类型类解决的令人满意的方法,但是我觉得这些类型类的导入不应该必须具有上下文的静态知识。 我在这里缺少什么吗? (我假设他们没有使用 scalaz.concurrent.Future)

【问题讨论】:

    标签: scala monads future scalaz


    【解决方案1】:

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

    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

    【讨论】:

    • 请注意,foo 只需要 Applicative,这应该在此处用作类型约束。我选择Monad 只是因为问题。
    猜你喜欢
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多