Standard Library Utilities
Either Fallback
Represent success or a named error without throwing.
Either Fallback
EitherFallback.scala
object Main {
def main(args: Array[String]): Unit = {
val ok =
val result: Either[String, Int] = if (ok) Right(42) else Left("missing")
val value = result.getOrElse(0)
val status = if (result.isRight) "ok" else "fallback"
println("status=" + status)
println("value=" + value)
}
}
object Main {
def main(args: Array[String]): Unit = {
val ok =
val result: Either[String, Int] = if (ok) Right(42) else Left("missing")
val value = result.getOrElse(0)
val status = if (result.isRight) "ok" else "fallback"
println("status=" + status)
println("value=" + value)
}
}
either
`Either[String, Int]` stores either an error label on the left or a useful value on the right. `getOrElse` supplies a fallback when the value is missing.