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

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

Returns true if the result is Ok.

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

Returns true if the result is Err.

fnexpectexpect(res : Result(a, e), msg : String) : a#

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

fnunwrapunwrap(res : Result(a, e)) : a#

Extracts the Ok value, panicking if Err.

fnunwrap_errunwrap_err(res : Result(a, e)) : 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.

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.

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.

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.

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.

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

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