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

Scalaz - Unable to unapply type StateT[Future, Foo, Bar]

我有一个 l 的列表 StateT[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@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@265a640

这让我认为这一定与 StateTFuture 上输入有关,也许 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@4c167067

在这种特殊情况下,Unapply 需要 FutureApplicative 实例并且由 this method 提供,这需要 ExecutionContext