6

通常 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

4

1 回答 1

2

好的,这有效:

scala> import scalaz._, Scalaz._, Unapply._
import scalaz._
import Scalaz._
import Unapply._

scala> val unapply = unapplyMAB2[Monad, State, Int, Unit](IndexedStateT.stateMonad[Int])
unapply: scalaz.Unapply[scalaz.Monad,scalaz.State[Int,Unit]]{type M[X] = scalaz.State[Int,X]; type A = Unit} = scalaz.Unapply_0$$anon$13@53a6f572

scala> List(1, 2, 3).traverseU((i: Int) => unapply.TC.pure(i))
res0: scalaz.IndexedStateT[scalaz.Id.Id,Int,Int,List[Int]] = scalaz.IndexedStateT$$anon$10@737c45ee

事实上,traverse在这种情况下只是普通的旧作品:

scala> List(1, 2, 3).traverse((i: Int) => unapply.TC.pure(i))
res1: unapply.M[List[Int]] = scalaz.IndexedStateT$$anon$10@73c622ec

我想发生了什么是我需要Unapply在范围内拥有实例才能知道该TC字段所指的类型。

于 2013-04-27T02:30:44.320 回答