Option
Some times values are optional, e.g. an expression like array_of_string.find(fn(s) -> s == "hallo") may or may not have a result.
To handle these there is an Option<type> wrapper.
E.g.
Option<int> // an optional integer
Option<string> // an optional string
Option<(int, string)> // an optional tuple of integer and string
Creation
The simplest way to create/instantiate an optional value is to use the builtin Some and None functions:
let maybe_a : Option<int> = Some(2345)
let maybe_b : Option<int> = None()
in which case
maybe_ais an optional integer that has the value2345(i.e. is defined)maybe_bis an optional integer that has no value (i.e. is not defined)
Alternative an optional value can be create as the result of an if-expression
let maybe_a = if (b < 0) 2345
in which case
if
b < 0is truemaybe_awill be an optional integer with value2345if
b < 0is not truemaybe_awill be an optional integer with no value
Accessing the value
Option has the following properties to access the value directly:
optional_value.defineda bool indicating if there is a value or notoptional_value.valuethe actual value, if there is oneBe careful using this! Accessing the value of an option with
defined == falsewill most likely lead to unexpected behaviour. At the very least wrap this in an if condition like
if (optional_value.defined) {
optional.value // use it in some way
}
A better way to access an optional value is to use the | operator:
let maybe_a : Option<int> = Some(2345)
let maybe_b : Option<int> = None()
maybe_a | 1234 // will be integer 2345 since maybe_a is defined
maybe_b | 1234 // will be integer 1234 since maybe_b is not defined