Result
Import Result for operations with an explicit success or error branch.
Type and constructors
Result
Result(e, a) contains an error of type e or a successful value of type a.
Err
The error branch.
Ok
The success branch. Both constructors are available to patterns.
Transforming
map
Transforms the value inside Ok and preserves Err unchanged.
mapError
Transforms the value inside Err and preserves Ok unchanged.
andThen
Calls the function for Ok and returns its result without nesting. Err skips
the function and passes through.
recover
Calls the recovery function for Err. Ok skips recovery and keeps its value.
The recovery may change the error type.
All transformation operations are O(1) apart from the callback.
Defaults and inspection
withDefault
Uses the first argument only for Err.
isOk
isErr
Conversion
toMaybe
Converts Ok value to Just value and discards an error as Nothing.
errorToMaybe
Converts Err error to Just error and discards a success as Nothing.
fromMaybe
Converts Just value to Ok value. Nothing becomes Err containing the
error supplied first.
Conversions are O(1). Use Maybe when absence needs no error value.
Generic methods
Importing Result supplies Equatable for both alternatives and Mappable/Reducible for Result(error). Mapping preserves Err; folds skip Err and visit the single Ok value.