2

monad 转换器XorT继承自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实例的最佳方法吗?

4

0 回答 0