Result
A result is :Ok(value) or :Err(error), typed Result<T, E>. The alias is built in, so every
annotation can use it.
@alias Result<T, E> {:Ok(T) | :Err(E)}| Function | Summary |
|---|---|
Result.is_ok |
Whether the result is :Ok. |
Result.is_err |
Whether the result is :Err. |
Result.with_default |
The :Ok value, or default for an :Err. |
Result.map |
Applies f to an :Ok value and leaves an :Err alone. |
Result.map_err |
Applies f to an :Err value and leaves an :Ok alone. |
Result.map_both |
Applies on_ok to an :Ok value or on_err to an :Err value. |
Result.map2 |
Combines two :Ok values with f, or returns the first :Err. |
Result.and_then |
Chains a step that can fail: f gets the :Ok value and returns the next result. |
Result.on_err |
Recovers from an :Err: f gets the error and returns the next result. |
Result.is_ok
Section titled “Result.is_ok”Result.is_ok <- function<T, E>(result: Result<T, E>) -> logical#: <T, E> fn(result: Result<T, E>) -> logicalResult.is_ok <- function(result)Whether the result is :Ok.
Result.is_ok(:Ok(1L))#> TRUEResult.is_err
Section titled “Result.is_err”Result.is_err <- function<T, E>(result: Result<T, E>) -> logical#: <T, E> fn(result: Result<T, E>) -> logicalResult.is_err <- function(result)Whether the result is :Err.
Result.is_err(:Ok(1L))#> FALSEResult.with_default
Section titled “Result.with_default”Result.with_default <- function<T, E>(result: Result<T, E>, default: T) -> T#: <T, E> fn(result: Result<T, E>, default: T) -> TResult.with_default <- function(result, default)The :Ok value, or default for an :Err.
Result.with_default(:Err(:Missing), 0L)#> 0LResult.map
Section titled “Result.map”Result.map <- function<A, B, E>(result: Result<A, E>, f: fn(A) -> B) -> Result<B, E>#: <A, B, E> fn(result: Result<A, E>, f: fn(A) -> B) -> Result<B, E>Result.map <- function(result, f)Applies f to an :Ok value and leaves an :Err alone.
Result.map(:Ok(2L), function(x) x * 10L)#> :Ok(20L)Result.map_err
Section titled “Result.map_err”Result.map_err <- function<T, A, B>(result: Result<T, A>, f: fn(A) -> B) -> Result<T, B>#: <T, A, B> fn(result: Result<T, A>, f: fn(A) -> B) -> Result<T, B>Result.map_err <- function(result, f)Applies f to an :Err value and leaves an :Ok alone.
Result.map_err(:Err("disk full"), function(e) :IoError(e))#> :Err(:IoError("disk full"))Result.map_both
Section titled “Result.map_both”Result.map_both <- function<A, B, C, D>(result: Result<A, B>, on_ok: fn(A) -> C, on_err: fn(B) -> D) -> Result<C, D>#: <A, B, C, D> fn(result: Result<A, B>, on_ok: fn(A) -> C, on_err: fn(B) -> D) -> Result<C, D>Result.map_both <- function(result, on_ok, on_err)Applies on_ok to an :Ok value or on_err to an :Err value.
Result.map_both(:Err(404L), function(x) x, function(code) Str.concat("HTTP ", Num.to_str(code)))#> :Err("HTTP 404")Result.map2
Section titled “Result.map2”Result.map2 <- function<A, B, C, E, F>(first: Result<A, E>, second: Result<B, F>, f: fn(A, B) -> C) -> Result<C, E | F>#: <A, B, C, E, F> fn(first: Result<A, E>, second: Result<B, F>, f: fn(A, B) -> C) -> Result<C, E | F>Result.map2 <- function(first, second, f)Combines two :Ok values with f, or returns the first :Err.
Result.map2(Str.to_integer("6"), Str.to_integer("7"), function(a, b) a * b)#> :Ok(42L)Result.and_then
Section titled “Result.and_then”Result.and_then <- function<A, B, E, F>(result: Result<A, E>, f: fn(A) -> Result<B, F>) -> Result<B, E | F>#: <A, B, E, F> fn(result: Result<A, E>, f: fn(A) -> Result<B, F>) -> Result<B, E | F>Result.and_then <- function(result, f)Chains a step that can fail: f gets the :Ok value and returns the next result. The error type
of the chain joins both, so a step can fail with a reason of its own.
Result.and_then(Str.to_integer("-3"), function(n) if (n > 0L) :Ok(n) else :Err(:NotPositive))#> :Err(:NotPositive)Result.on_err
Section titled “Result.on_err”Result.on_err <- function<T, A, B>(result: Result<T, A>, f: fn(A) -> Result<T, B>) -> Result<T, B>#: <T, A, B> fn(result: Result<T, A>, f: fn(A) -> Result<T, B>) -> Result<T, B>Result.on_err <- function(result, f)Recovers from an :Err: f gets the error and returns the next result.
Result.on_err(Str.to_integer("x"), function(e) :Ok(0L))#> :Ok(0L)