Options
Option types
An option can be empty or contain a single value.
An option type is written as foo?.
() returns an empty option.
Any value can implicitly convert to a non-empty option containing it.
So any nat can implicitly convert to a nat?.
Options are useful for functions that might not have a result:
"if" and options
To get the value inside an option, test the option in the condition of an if.
Inside the if body, the option will implicitly convert to a non-option.
The below example uses first from the standard library,
which returns the first element in an array if one exists.
The local can be introduced inside the if.
You can even introduce a local in the left side of an and expression.
Option operations
a! forces an option.
This gets the inner value or throws an exception if the option is empty.
If the ! comes after a function name as in half! 4,
it forces function's return value.
a or b is analogous to the boolean or.
It first evaluates a. If that is a non-empty option, it is returned.
Otherwise, it evaluates b.
In other words, a or b or... evaluates options and returns the first non-empty one.
a else b is like a or b, but expects b to be non-optional.
While a or b takes two options of the same type, a else b only takes an option on the left.
So the else expression is non-optional, since it always has b to fall back on.
Optional calls
?foo calls foo if all of its arguments are non-empty options.
Otherwise it skips the call and returns an empty option.
?foo can also return void if that is the expected type.
This also works with dot-calls: x.?foo
It works for subscript calls too:
Useful option functions
from-option converts an option to any collection.