通常 ScalazUnapply
做得很好,但它似乎在这里分解为traverseU
:
scala> import scalaz._, Scalaz._, Unapply._
import scalaz._
import Scalaz._
import Unapply._
scala> val stateMonadInstance = unapplyMAB2[Monad, State, Int, Unit](IndexedStateT.stateMonad[Int]).TC
stateMonadInstance: scalaz.Monad[[X]scalaz.IndexedStateT[[+X]X,Int,Int,X]] = scalaz.StateTInstances1$$anon$1@27c591e1
scala> List(1, 2, 3).traverseU((i: Int) => stateMonadInstance.pure(i))
<console>:18: error: Unable to unapply type `scalaz.IndexedStateT[[+X]X,Int,Int,Int]` 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.IndexedStateT[[+X]X,Int,Int,Int]])
List(1, 2, 3).traverseU((i: Int) => stateMonadInstance.pure(i))
^
该traverseS
方法似乎已创建为解决此问题的方法,无论它是什么:
scala> List(1, 2, 3).traverseS((i: Int) => stateMonadInstance.pure(i))
res11: scalaz.State[Int,List[Int]] = scalaz.package$State$$anon$3@2634d0e2
但是我正在尝试编写一个对于所讨论的单子来说是通用的库,所以这不是一个很好的选择。有谁知道阻止它工作的确切问题是什么,以及是否有不需要特殊情况的解决方法State
?