Documentation
¶
Overview ¶
Package result implements a Result{value, error} "sum type" that has a value only when error is nil.
Note that in many cases, it is more idiomatic for a function to return a naked (value, error). Use WrapFunc to convert such a function to return a Result type.
For examples, see the sibling "maybe" package.
Index ¶
- func Lift[A any, B any](f func(a A) B) func(a A) Result[B]
- func UnwrapFunc[A any, B any](f func(a A) Result[B]) func(a A) (B, error)
- func WrapFunc[A any, B any](f func(a A) (B, error)) func(a A) Result[B]
- type Result
- func Apply[A any, B any](a Result[A], f Result[func(a A) B]) Result[B]
- func Error[V any](err error) Result[V]
- func Map[A any, B any](a Result[A], f func(a A) B) Result[B]
- func New[V any](value V, err error) Result[V]
- func Some[V any](value V) Result[V]
- func Then[A any, B any](a Result[A], f func(a A) Result[B]) Result[B]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UnwrapFunc ¶
UnwrapFunc converts a function of the form f(x) => Result{value, error} to the form f(x) => (value, error)
Types ¶
type Result ¶
Result is a (value, error) "sum type" that has a value only when error is nil.
func Apply ¶
Apply applies Maybe(f(x)) => y. That is, function f is itself a Result. If either a or f are errors, returns an Error. Otherwise, returns Some(f(a.Value)).
If both a and f are errors, returns either error.
func Map ¶
Map applies a function f(x) => y. If a is an error, returns an error. Otherwise, returns Some(f(a.Value)).
For a function that applies f(x) => Maybe(y) instead, see Then.
func New ¶
New returns a Result. It is syntax sugar for Result{value, error}. If error is a known constant, use Some or Error instead.
func Then ¶
Then (a.k.a. "flatMap") applies a function f(x) => Maybe(y). If a is Nothing, returns Nothing, otherwise returns f(a.Value).
For a function that applies f(x) => y instead, see Map.
func (Result[V]) Else ¶
func (r Result[V]) Else(v V) V
Else returns the Results's value (if error is nil), otherwise returns the provided argument instead.
func (Result[V]) Filter ¶
Filter examines a Result. If error is nil and f(value) returns true, returns Some(value), otherwise returns Error(error).
func (Result[V]) Match ¶
Match examines a Result and returns true if it is not an error and if the provided predicate returns true for the value.
func (Result[V]) Must ¶
func (r Result[V]) Must() V
Must returns a Result's value. If the Result is an error, panics with the error.