这是我之前的 question 的后续内容:
假设我正在重构这样的函数:
def check(ox: Option[Int]): Unit = ox match {
case None => throw new Exception("X is missing")
case Some(x) if x < 0 => throw new Exception("X is negative")
case _ => ()
}
我正在编写一个新的纯函数doCheck
来返回Unit
或异常。
case class MissingX() extends Exception("X is missing")
case class NegativeX(x: Int) extends Exception(s"$x is negative")
import scalaz._, Scalaz._
type Result[A] = Excepiton \/ A
def doCheck(ox:Option[Int]): Result[Unit] = for {
x <- ox toRightDisjunction MissingX()
_ <- (x >= 0) either(()) or NegativeX(x)
} yield ()
然后从check调用它
def check(ox:Option[Int]): Unit = doCheck(ox) match {
case -\/(e) => throw e
case _ => ()
}
这有道理吗?像这样实现 doCheck
会更好吗?
def doCheck(ox:Option[Int]): Result[Int] = for {
x1 <- ox toRightDisjunction MissingX()
x2 <- (x1 >= 0) either(x1) or NegativeX(x1)
} yield x2
如何用cats
实现它?
最佳答案
你会在猫身上做几乎相同的事情,只有猫本身没有 Boolean => Xor[A, B]
语法,如 () 或 ()
来自 Scalaz 。
import cats.data.Xor
import cats.implicits._
def doCheck(ox: Option[Int]): Xor[Exception, Unit] =
ox.toRightXor(MissingX()).flatMap(x => if(x > 0) ().right else NegativeX(x).left)
您可以使用mouse ,它为猫提供了类似的语法帮助器:
import com.github.benhutchison.mouse.boolean._
ox.toRightXor(MissingX()).flatMap(x => (x > 0).toXor(NegativeX(x), ()))
Xor
也有方法 ensure
来执行类似的操作,但如果谓词不成立,它不会让您访问该元素。如果您不需要 NegativeX
的 x
,您可以编写:
ox.toRightXOr(MissingX()).ensure(Negative())(_ > 0).void
关于scala - 如何使用 Scalaz 或 Cats 重构抛出异常的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915718/