Skip to content

Errors

Option<A> represents a value that may be absent: Some(value) or None. At runtime those are {:some, value} and :none, so Some(None) remains distinct from None and from nil.

Expected failure uses Result<Success, Error>: Ok(value) or Error(reason). with chains successful results; each <- expression must produce a Result, the final expression is wrapped in Ok, and the first error passes through unchanged.

module errors
pub fn unwrap_or(option: Option<Integer>, fallback: Integer) -> Integer {
match option {
Some(value) -> value
None -> fallback
}
}
pub fn double(n: Integer) -> Result<Integer, :badarg> {
with {
value <- parse(n)
value * 2
}
}
fn parse(n: Integer) -> Result<Integer, :badarg> {
match n >= 0 {
true -> Ok(n)
false -> Error(:badarg)
}
}
pub fn double_or(n: Integer, fallback: Integer) -> Integer {
match double(n) {
Ok(value) -> value
Error(_) -> fallback
}
}

Elixir APIs that return A.t() | nil should be typed as A | nil, not as Option<A>. Use gale_std.option.from_nil and gale_std.option.to_nil explicitly at that boundary. A Result<A, Never> cannot contain Error, because Never is the empty type.