March Docs

Result

Result module: operations on Result(a, e) = Ok(a) Err(e)

Result is the primary error-handling mechanism. Use it for any operation that can reasonably fail. Panics are reserved for programmer errors (invariant violations).

Functions

fncollectcollect(results : List(Result(a, e))) : Result(List(a), e)#

Collects a list of Results into a Result of a list. Returns the first Err encountered, or Ok with all values if all are Ok.

fnexpectexpect(res : {Result(a, e) | is_Ok(_)}, msg : String) : a#

Extracts the Ok value, panicking with the given message if Err. Use when you are certain the result is Ok.

The panic message includes the underlying Err value so callers can
see *why* the assumption was violated, not just that it was.
fnflat_mapflat_map(res : Result(a, e), f : a -> Result(b, e)) : Result(b, e)#

Applies f (which returns a Result) to the Ok value, flattening the result.

fnis_erris_err(res : Result(a, e)) : Bool#

Returns true if the result is Err.

fnis_okis_ok(res : Result(a, e)) : Bool#

Returns true if the result is Ok.

fnmapmap(res : Result(a, e), f : a -> b) : Result(b, e)#

Applies f to the Ok value, leaving Err unchanged.

fnmap_errmap_err(res : Result(a, e), f : e -> f2) : Result(a, f2)#

Applies f to the Err value, leaving Ok unchanged.

fnor_elseor_else(res : Result(a, e), f : e -> Result(a, f2)) : Result(a, f2)#

Returns f(err) if Err, or the original Ok value.

fnto_optionto_option(res : Result(a, e)) : Option(a)#

Converts Result(a, e) to Option(a), discarding the error.

fnunwrapunwrap(res : {Result(a, e) | is_Ok(_)}) : a#

Extracts the Ok value, panicking if Err.

fnunwrap_errunwrap_err(res : {Result(a, e) | is_Err(_)}) : e#

Extracts the Err value, panicking if Ok.

fnunwrap_orunwrap_or(res : Result(a, e), default : a) : a#

Returns the Ok value, or default if Err.