ScalaにもEitherあったんだな、と。
object Main { def foo(x : Int) : Either[String, Int] = x match { case 0 => Left("invalid arguments!") case _ => Right(x) } def result(a : Either[String, Int]) : Unit = a match { case Left(msg) => println("Error : " + msg) case Right(x) => println(x) } def main(args: Array[String]) { result(foo(0)) result(foo(1)) } }
Error : invalid arguments! 1