Errors: Result and Option
Report failure and missing values with Result and Option, match on them with case, and pass errors up with the question mark.
Things go wrong in real programs. A number cannot be divided by zero. A value
that you look for is not there. Actus has no hidden exceptions. Instead, a
verb says in its return type that it can fail, and the caller has to deal
with that. Two types are built in for this: Result and Option.
Result[T, E]is eitherOkwith a value of typeT, orErrwith an error of typeE. Use it when something can fail and you want to say why.Option[T]is eitherSomewith a value of typeT, orNone. Use it when a value may be absent. There is no null in Actus.
Both are enums, like the ones in
Structs, enums and case, and you take them
apart with case.
Return a Result
The verb divide below can fail, so it returns Result[Int, DivideError]. A
success is Ok with the answer. A failure is Err with a variant of our own
DivideError enum:
In divide, the result is built with the full type name,
Result[Int, DivideError].Err(...). In main, case dat first looks inside
the result. The arm Result.Ok(value) names the answer, and the arm
Result.Err(_) handles the failure. The _ means "there is an error, and I
do not need its details here".
case dat first uses up first. The case takes the value, and you cannot
use first again afterwards. The end of this page shows how to look at a
value without using it up.
Because a case must cover every variant, you cannot forget the failure. The
compiler rejects a case without the Err arm, as the next section shows
for Option.
Return an Option
When there is no error to explain and a value is simply missing, use
Option. This verb gives back the first even number of two:
Some(n) carries the number, and None carries nothing. As with Result,
the case has to handle both:
You also cannot use an Option[Int] as if it were an Int. The value may be
missing, so you must go through case:
Pass errors up with the question mark
Often a verb cannot do anything useful with an error and only wants to pass it
to its caller. Put ? after a call that returns a Result. If the result is
Ok, you get the value. If it is Err, the verb returns that error right
away.
In half_of_quotient, the line
erg quotient = divide(...)?; gives the answer when divide succeeds. When
divide fails, half_of_quotient stops and returns the same Err, so the
second report prints division by zero. Inside a verb whose return type is a
Result, you can also write Ok(...) and Err(...) without the type.
You can also write ? inside a larger expression, for example in the
condition of an if. If the call fails, the verb returns the error at that
point:
The ? has two conditions. The verb that uses it must return a Result, and
the error types must be the same.
In the second program, first fails with a ParseError and second can only
fail with an Int. Actus does not convert one error type into the other for
you.
A result is never thrown away
A Result carries an error, and an Option may hold no value. So Actus does
not let you ignore either one. A call that returns a Result or an Option
must be used: bind it and look at it, return it, pass it on, or propagate it
with ?. If you call it as a statement on its own, the compiler stops you
with E1120:
The same error appears when you bind the value and then never use the binding. It is the same mistake: the value was never looked at. The message names the binding.
There are three ways out. Look at the value with case, as you did above.
Pass the error up with ? when the verb returns a Result with the same
error type. Or, when you really do not care about the outcome, say so with
drop:
drop(first_even(...)); is the explicit discard. It ends the value on
purpose, and it shows the reader that the choice was made. A ? on a call
statement, such as first(n: abs n)?;, also counts as using the result,
because the error goes to the caller.
Looking without using it up
case dat consumes the value, so a second case on the same variable is an
error:
When you want to look at a value more than once, use case abs. It reads the
value and leaves it where it is:
Errors and the exit status
main cannot return a Result. It returns nothing or an Int, so you deal
with errors inside main, and then choose the exit status:
For example, report the failure on the error stream and return a status that
is not 0. The error stream, eprintln, is described in
Printing and input/output.
What you learned
Result[T, E]isOk(value)orErr(error).Option[T]isSome(value)orNone.- A verb that can fail says so in its return type.
casetakes the value apart and must cover both variants. Usecase datto use it up,case absto look at it.?returns the error of a failed call to the caller. The verb must return aResultwith the same error type.- A
Resultor anOptionis never thrown away. Look at it, return it, propagate it with?, or end it on purpose withdrop(...). Otherwise the compiler reportsE1120. mainreturns nothing or anInt. Handle errors inmainand return a status.
Next: Printing and input/output.