XorT
monad转换器继承自MonadError
,它暴露了错误处理功能。我想为MonadError
创建WriterT[XorT ...]
类型类的实例,但我未能实现handleErrorWith
方法。
type WriterValid[L, E, T] = WriterT[XorT[Id, E, ?], L, ?]
val writerValidMonad = new MonadError[WriterValid[L, E, ?], E] {
private val monadT = data.WriterT.writerTMonad[XorT[Id, E, ?], L]
def raiseError[A](e: E): WriterValid[L, E, A] =
cats.data.WriterT[Valid[E,?],L,A](validMonad.raiseError[(L,A)](e))
def handleErrorWith[A](fa: WriterValid[L, E, A])(f: (E) => WriterValid[L, E, A]): WriterValid[L, E, A] =
???
def pure[A](x: A): WriterValid[L, E, A] =
monadT.pure(x)
def flatMap[A, B](fa: WriterValid[L, E, A])(f: (A) => WriterValid[L, E, B]): WriterValid[L, E, B] =
monadT.flatMap(fa)(f)
}
这是否可以轻松完成?这也是创建MonadError
实例的最佳方法吗?