나는이 같은 함수 리팩토링하고 가정하자 :
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
그것을 구현하는 방법?
왜 당신은 여전히'check'에서 예외를 발생합니까? –
@PeterNeyens'check' 밖에서 모든 코드를 변경하고 싶지는 않습니다. – Michael