Options and Error Handling
Result Labels
Carry success or error as a labeled result.
Result Labels
ResultLabels.scala
object Main {
def main(args: Array[String]): Unit = {
val divisor =
val ok = divisor != 0
val value = if (ok) 100 / divisor else 0
val outcome = if (ok) "ok:" + value else "error:divide-by-zero"
println("ok=" + ok)
println("outcome=" + outcome)
}
}
object Main {
def main(args: Array[String]): Unit = {
val divisor =
val ok = divisor != 0
val value = if (ok) 100 / divisor else 0
val outcome = if (ok) "ok:" + value else "error:divide-by-zero"
println("ok=" + ok)
println("outcome=" + outcome)
}
}
object Main {
def main(args: Array[String]): Unit = {
val divisor =
val ok = divisor != 0
val value = if (ok) 100 / divisor else 0
val outcome = if (ok) "ok:" + value else "error:divide-by-zero"
println("ok=" + ok)
println("outcome=" + outcome)
}
}
result-labels
A result can be modeled as a success or error label with a value. This guards a risky step, like division, without an exception.