由于Free
不是Scalaz 7.1.5中的monad实例,因此我无法使用Applicative
、Apply
等中定义的有用方法。
/* ref - http://tpolecat.github.io/assets/sbtb-slides.pdf */
import Free._, Coyoneda._
type ResultSetIO[A] = FreeC[ResultSetOp, A]
val next : ResultSetIO[Boolean] = liftFC(Next)
def getString(index: Int): ResultSetIO[String] = liftFC(GetString(index))
def getInt(index: Int) : ResultSetIO[Int] = liftFC(GetInt(index))
def close : ResultSetIO[Unit] = liftFC(Close)
// compile errors
def getPerson1: ResultSetIO[Person] =
(getString(1) |@| getInt(2)) { Person(_, _)}
def getNextPerson: ResultSetIO[Person] =
next *> getPerson
def getPeople(n: Int): ResultSetIO[List[Person]] =
getNextPerson.replicateM(n) // List.fill(n)(getNextPerson).sequence
erorr消息是,
Error:(88, 19) value |@| is not a member of free.JDBC.ResultSetIO[String]
(getString(1) |@| getInt(2)) { Person(_, _)}
^
Error:(91, 10) value *> is not a member of free.JDBC.ResultSetIO[Boolean]
next *> getPerson
^
Error:(94, 19) value replicateM is not a member of free.JDBC.ResultSetIO[free.Person]
getNextPerson.replicateM(n) // List.fill(n)(getNextPerson).sequence
^
我应该为Free
实现monad实例吗?
implicit val resultSetIOMonadInstance = new Monad[ResultSetIO] {
override def bind[A, B](fa: ResultSetIO[A])(f: (A) => ResultSetIO[B]): ResultSetIO[B] =
fa.flatMap(f)
override def point[A](a: => A): ResultSetIO[A] =
Free.point[CoyonedaF[ResultSetOp]#A, A](a)
}
或者,我是不是错过了什么?(例如导入)
这只是Scala编译器对类型别名的挑剔。你有两个选择(或者至少两个选择——可能还有其他合理的解决方案)。第一种是对类型别名进行稍微不同的分解。取而代之的是:
type ResultSetIO[A] = FreeC[ResultSetOp, A]
你写的是:
type CoyonedaResultSetOp[A] = Coyoneda[ResultSetOp, A]
type ResultSetIO[A] = Free[CoyonedaResultSetOp, A]
然后Monad[ResultSetIO]
会编译得很好。您需要为|@|
、*>
和replicateM
额外导入一个:
import scalaz.syntax.applicative._
另一种选择是保持FreeC
的原样,自己定义monad实例,因为scalac找不到它。幸运的是,你可以比按照你的建议写出来更简单一点:
implicit val monadResultSetIO: Monad[ResultSetIO] =
Free.freeMonad[({ type L[x] = Coyoneda[ResultSetOp, x] })#L]
我更喜欢第一种方法,但你选择哪一种并不重要。
为了方便起见,这里有一个简化的完整工作示例:
sealed trait ResultSetOp[A]
case object Next extends ResultSetOp[Boolean]
case class GetString(index: Int) extends ResultSetOp[String]
case class GetInt(index: Int) extends ResultSetOp[Int]
case object Close extends ResultSetOp[Unit]
import scalaz.{ Free, Coyoneda, Monad }
import scalaz.syntax.applicative._
type CoyonedaResultSetOp[A] = Coyoneda[ResultSetOp, A]
type ResultSetIO[A] = Free[CoyonedaResultSetOp, A]
val next: ResultSetIO[Boolean] = Free.liftFC(Next)
def getString(index: Int): ResultSetIO[String] = Free.liftFC(GetString(index))
def getInt(index: Int): ResultSetIO[Int] = Free.liftFC(GetInt(index))
def close: ResultSetIO[Unit] = Free.liftFC(Close)
case class Person(s: String, i: Int)
def getPerson: ResultSetIO[Person] = (getString(1) |@| getInt(2))(Person(_, _))
def getNextPerson: ResultSetIO[Person] = next *> getPerson
def getPeople(n: Int): ResultSetIO[List[Person]] = getNextPerson.replicateM(n)
这将与7.1.5一起编译。
为了完整起见,还有第三种方法,那就是定义一些Unapply
机制来帮助编译器找到FreeC
版本的实例(Rob Norris负责这段代码,我刚刚对其进行了分类):
implicit def freeMonadC[FT[_[_], _], F[_]](implicit
ev: Functor[({ type L[x] = FT[F, x] })#L]
) = Free.freeMonad[({ type L[x] = FT[F, x] })#L]
implicit def unapplyMMFA[TC[_[_]], M0[_[_], _], M1[_[_], _], F0[_], A0](implicit
TC0: TC[({ type L[x] = M0[({ type L[x] = M1[F0, x] })#L, x] })#L]
): Unapply[TC, M0[({ type L[x] = M1[F0, x] })#L, A0]] {
type M[X] = M0[({ type L[x] = M1[F0, x] })#L, X]
type A = A0
} = new Unapply[TC, M0[({ type L[x] = M1[F0, x] })#L, A0]] {
type M[X] = M0[({ type L[x] = M1[F0, x] })#L, X]
type A = A0
def TC = TC0
def leibniz = Leibniz.refl
}
这允许您使用FreeC
,而无需每次都定义monad实例。不过,我仍然认为放弃FreeC
并使用Free
是一个更好的主意。