scalascalazscalaz7

Create a OptionT[Future, A] from a lower-kinded type


I'm pretty new to scalaz, and I'm trying to figure out to convert various types to monad transformers.

I'm stuck on trying to convert a Int to a OptionT[Future, Int], or even to EitherT[Future, String, Int].

I found a bunch of tutorials/SO answers that explain how to do this using point, but for some reason I can't compile them.

For instance, this snippet from here:

1.point[({ type L[x] = EitherT[Future, String, x] })#L]

Error:(9, 9) could not find implicit value for evidence parameter of type scalaz.Applicative[[x]scalaz.EitherT[scala.concurrent.Future,String,x]]

Another from Scalaz Monad Transformers

type Result[A] = OptionT[Future, A]
"".point[Result]

Error:(8, 10) could not find implicit value for evidence parameter of type scalaz.Applicative[A$A35.this.Result]

I believe this one should work as well, but it says method liftM is not a member of Future[Int]:

1.point[Future].liftM[OptionT]    //doesnt compile
1.point[List].liftM[OptionT]      //compiles

All these examples fail, but they compile if I replace Future with, say, List. Right now, this is the only way that works for me, but it's a bit verbose - I'd really like to be able to use point instead:

OptionT(Future.successful(1.some))

Why is this not compiling? Was the applicative/monad for Future removed from scalaz in a recent version?

I'm using scala 2.11.7 and scalaz 7.1.3. For what it's worth, these are my imports:

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

Solution

  • Importing an ExecutionContext will make your solutions compile, see scalaz.std.scalaFuture.

    import scala.concurrent.ExecutionContext.Implicits.global
    
    type Result[A] = OptionT[Future, A]
    "".point[Result]
    // Result[String] = OptionT(scala.concurrent.impl.Promise$DefaultPromise@5e155fc)
    
    1.point[Future].liftM[OptionT]
    // scalaz.OptionT[scala.concurrent.Future,Int] = OptionT(scala.concurrent.impl.Promise$DefaultPromise@60821af9)