Scalaz - 无法取消应用类型 StateT[Future, Foo, Bar]

标签 scala scalaz

我有一个 list lStateT[Id, MyState, Boolean] .

有了它,我能够执行以下操作:

case class MyState (s: String)

val startState = MyState ("s")

val l: List[StateT[Id, MyState, Boolean]] = ...

val failed = l.sequenceU.map { x => }
 .map { _.foldMap (identity)(Monoid.instance (_ | _, false)) }
 .eval (startState)

这会将状态逐一通过列表中的所有内容进行管道传输,然后使用按位或 (|) 组合所有结果。

我现在想将我的列表更改为 StateT[Future, MyState, Boolean] 类型,但是我正在努力让它编译。编译器无法计算出我需要做什么才能编译它。编译器告诉我,我需要能够编译 implicitly[Unapply[Applicative, StateT[Future, MyState, MyResult]]] ,现在我不能,我的问题是如何隐式定义我需要什么?

以下内容重现了 REPL 上的错误:

scala> import scalaz._
import scalaz._

scala> import scalaz.Id._
import scalaz.Id._

scala> import scala.concurrent.Future
import scala.concurrent.Future

scala> case class MyState (s: String)
defined class MyState

scala> case class MyResult (r: String)
defined class MyResult

scala> implicitly[Unapply[Applicative, StateT[Id, MyState, MyResult]]]
res0: scalaz.Unapply[scalaz.Applicative,scalaz.StateT[scalaz.Id.Id,MyState,MyResult]] = scalaz.Unapply_2$$anon$5@5443fcf1

scala> implicitly[Unapply[Applicative, StateT[Future, MyState, MyResult]]]
<console>:55: error: Unable to unapply type `scalaz.StateT[scala.concurrent.Future,MyState,MyResult]` into a type constructor of kind `M[_]` that is classified by the type class `scalaz.Applicative`
1) Check that the type class is defined by compiling `implicitly[scalaz.Applicative[<type constructor>]]`.
2) Review the implicits in object Unapply, which only cover common type 'shapes'
(implicit not found: scalaz.Unapply[scalaz.Applicative, scalaz.StateT[scala.concurrent.Future,MyState,MyResult]])
              implicitly[Unapply[Applicative, StateT[Future, MyState, MyResult]]]
                        ^

有趣的是,编译如下:

 scala> implicitly[Unapply[Applicative, StateT[Option, MyState, MyResult]]]
res2: scalaz.Unapply[scalaz.Applicative,scalaz.StateT[Option,MyState,MyResult]] = scalaz.Unapply_2$$anon$5@265a640

这让我认为这一定与它有关 StateT正在输入Future ,也许是 Future 的东西没有定义?

我正在使用 Scalaz 7.0.5,我知道它没有 Monad[Future] ,但是我正在导入:

https://github.com/typelevel/scalaz-contrib/blob/v0.1.5/scala210/main/scala/Future.scala

此外,我已经使用 Scalaz 7.1.0 在 REPL 上对此进行了测试,并且得到了相同的错误。

任何帮助将不胜感激!

干杯!

最佳答案

这有点棘手,但是......您实际上需要在范围内有一个隐式的ExecutionContext:

scala> import scalaz.contrib.std.scalaFuture._
import scalaz.contrib.std.scalaFuture._

scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

scala> implicitly[Unapply[Applicative, StateT[Future, MyState, MyResult]]]
res14: scalaz.Unapply[scalaz.Applicative,scalaz.StateT[scala.concurrent.Future,MyState,MyResult]] = scalaz.Unapply_2$$anon$5@4c167067

在这种特殊情况下,Unapply 需要 FutureApplicative 实例,该实例由 this method 提供。 ,这需要一个 ExecutionContext

关于Scalaz - 无法取消应用类型 StateT[Future, Foo, Bar],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317163/

相关文章:

Scala map2 over tuple with inner monoid (or : how to do this simple thing but better? )

scala - Scala Apache Spark中DStream的输出内容

scala - 为什么这个谓词中的参数可以省略呢?

Scalaz 自动类型类解析

scala - 等效于 ScalaZ 中的新类型派生

scala - 将 List[ValidationNEL[String, Unit]] 转换为 ValidationNEL[String, Unit]

android - Android 上的 Scala 与 scala.concurrent.Future 不会报告系统错误/输出异常

scala - 没有绑定(bind) play.api.db.slick.DatabaseConfigProvider 的实现

scala - Scala 中的 SynchronizedSet 和 set 操作

scala - 如何定义在免费应用程序中使用的递归代数?