【问题标题】:Create a OptionT[Future, A] from a lower-kinded type从较低种类的类型创建 OptionT[Future, A]
【发布时间】:2015-08-28 10:42:08
【问题描述】:

我对 scalaz 还是很陌生,我正在尝试将各种类型转换为 monad 转换器。

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

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

比如这个来自here的sn-p:

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

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

另一个来自Scalaz Monad Transformers

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

所有这些示例都失败了,但是如果我将Future 替换为List,它们就会编译。目前,这是对我有用的唯一方法,但有点冗长 - 我真的希望能够使用 point 代替:

OptionT(Future.successful(1.some))

为什么不编译?在最近的版本中,Future 的应用程序/monad 是否已从 scalaz 中删除?

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

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

【问题讨论】:

    标签: scala scalaz scalaz7


    【解决方案1】:

    导入ExecutionContext 将使您的解决方案编译,请参阅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)
    

    【讨论】:

    • 准确!原来futureMonoid 需要一个隐式执行上下文......我希望那些教程已经提到了这个detail。
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2020-09-11
    • 2020-10-17
    相关资源
    最近更新 更多