Exception with ScalaZ OptionT
Consider the following code that ScalaZ uses OptionT
:
val answer = for {
customer <- optionT(function1(codeString))
customerId <- someOptionT(Future(Seq(function2(customer)))
alerts <- someOptionT(Future.sequence(function3(customerId))
} yield alerts
-
function1
returnsFuture[Option[Customer]]
-
function2
returnsString
-
function3
returnsSeq[Future[Option[String]]]
Meanwhile it someOptionT
def someOptionT[T](t: Future[T]): OptionT[Future, T] = optionT[Future](t.map(Some.apply))
I am getting the following exception on the last line of understanding for
(c alerts
):
could not find implicit value for parameter F: scalaz.Functor[scala.concurrent.Future]
Any idea why?
source to share
The error message is completely unclear, but the problem is exactly the same as if you wrote myFuture.map(whatever)
in the REPL without the implicit execution context in the scope.
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 provides Functor
(and a Monad
, etc.) for Future
, but it requires an execution context to get them. Import global
(or specify another way) and you should be fine.
source to share