0

我对 scalaz 很陌生,我正试图弄清楚将各种类型转换为 monad 转换器。

我一直在尝试将 a 转换Int为 a OptionT[Future, Int],甚至转换为EitherT[Future, String, Int].

我找到了一堆教程/SO 答案,它们解释了如何使用 来执行此操作point,但由于某种原因,我无法编译它们。

例如,这里的这个片段:

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

错误:(9, 9) 找不到类型的证据参数的隐式值scalaz.Applicative[[x]scalaz.EitherT[scala.concurrent.Future,String,x]]

另一个来自Scalaz Monad 变形金刚

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

错误:(8, 10) 找不到类型的证据参数的隐式值scalaz.Applicative[A$A35.this.Result]

我相信这个应该也可以,但是它说方法liftM不是以下成员Future[Int]

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

所有这些示例都失败了,但是如果我替换FutureList. 现在,这是对我有用的唯一方法,但它有点冗长 - 我真的很想能够使用point

OptionT(Future.successful(1.some))

为什么这不编译?Future在最近的版本中,是否从 scalaz 中删除了 applicative/monad ?

我正在使用 scala 2.11.7 和 scalaz 7.1.3。对于它的价值,这些是我的进口:

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

1 回答 1

7

导入 anExecutionContext将使您的解决方案编译,请参阅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)
于 2015-08-28T11:05:26.290 回答