Scala中使用“任一”的错误处理



这是我上一个问题的后续内容。假设我有以下功能:

type Result[A] = Either[String, A] // left is an error message
def f1(a: A): Result[B] = ...
def f2(b: B): Result[C] = ...
def f3(c: C): Result[D] = ...
def f(a: A): Result[D] = for {
  b <- f1(a).right
  c <- f2(b).right
  d <- f3(c).right
} yield d; 

还假设我想在错误消息中添加更多信息。

 def f(a: A): Result[D] = for {
  b <- { val r = f1(a); r.left.map(_ + s"failed with $a"); r.right }
  c <- { val r = f2(b); r.left.map(_ + s"failed with $a and $b"); r.right }
  d <- { val r = f3(c); r.left.map(_ + s"failed with $a, $b, and $c"); r.right } 
} yield d; 

代码看起来很难看。你建议如何改进代码?

代码看起来很难看,因为您在重复自己。

改为编写一个方法!或者扩展方法。其中之一,也许:

implicit class StringEitherCanAdd[A](private val e: Either[String, A]) extends AnyVal {
  def info(s: String): Either[String, A] = e.left.map(_ + s)
  def failed(a: Any*): Either[String, A] =
    if (a.length == 0) e
    else if (a.length == 1) e.left.map(_ + s"failed with ${a(0)}")
    else if (a.length == 2) e.left.map(_ + s"failed with ${a(0)} and ${a(1)}")
    else e.left.map(_ + s"failed with ${a.init.mkString(", ")}, and ${a.last}")
}

现在你只是

f1(a).info(s"failed with $a").right
f2(b).failed(a,b).right

最新更新