Cats中WriterTXorT monad转换器的MonadError类型类实例

时间:2016-04-14 20:18:09

标签: scala monad-transformers scala-cats

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实例的最佳方法吗?

0 个答案:

没有答案