【问题标题】:Exception with ScalaZ OptionTScalaZ OptionT 异常
【发布时间】:2015-07-10 10:24:07
【问题描述】:

考虑以下使用 ScalaZ 的 OptionT 的代码:

val answer = for {
      customer <- optionT(function1(codeString))
      customerId <- someOptionT(Future(Seq(function2(customer)))
      alerts <- someOptionT(Future.sequence(function3(customerId))
} yield alerts
  • function1 返回Future[Option[Customer]]
  • function2 返回String
  • function3 返回Seq[Future[Option[String]]]

同时这是someOptionT

def someOptionT[T](t: Future[T]): OptionT[Future, T] = optionT[Future](t.map(Some.apply))

我在for 理解的最后一行(使用alerts)得到以下异常:

could not find implicit value for parameter F: scalaz.Functor[scala.concurrent.Future]

知道为什么吗?

【问题讨论】:

    标签: scala future scalaz


    【解决方案1】:

    错误消息完全不清楚,但问题与您在 REPL 中编写 myFuture.map(whatever) 时完全一样,但范围内没有隐式执行上下文。

    scala> import scala.concurrent.Future
    import scala.concurrent.Future
    
    scala> def foo(myFuture: Future[Int]) = myFuture.map(_ + 1)
    <console>:8: error: Cannot find an implicit ExecutionContext. You might pass
    an (implicit ec: ExecutionContext) parameter to your method
    or import scala.concurrent.ExecutionContext.Implicits.global.
           def foo(myFuture: Future[Int]) = myFuture.map(_ + 1)
                                                        ^
    

    Scalaz 为Future 提供了一个Functor(和一个Monad 等),但是您必须有一个执行上下文才能获取它们。导入global(或提供其他方式)就可以了。

    【讨论】:

    • 我尝试了global 和 Play 的执行上下文,但都没有成功。奇怪。
    • 你的 Scalaz 进口是什么?如果你不去厨房水槽,你至少需要scalaz.std.scalaFuture._。
    • 是的,我刚刚注意到与看到您的回复相同的事情。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    相关资源
    最近更新 更多