result

package
v2.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2022 License: MIT Imports: 0 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lift

func Lift[A any, B any](
	f func(a A) B,
) func(a A) Result[B]

Lift converts a function of the form f(a) => b to the form f(a) => Result(b).

func UnwrapFunc

func UnwrapFunc[A any, B any](
	f func(a A) Result[B],
) func(a A) (B, error)

UnwrapFunc converts a function of the form f(x) => Result{value, error} to the form f(x) => (value, error)

func WrapFunc

func WrapFunc[A any, B any](
	f func(a A) (B, error),
) func(a A) Result[B]

WrapFunc converts a function of the form f(x) => (value, error) to the form f(x) => Result{value, error}.

Types

type Result

type Result[V any] struct {
	Value V
	Error error
}

Result is a (value, error) "sum type" that has a value only when error is nil.

func Apply

func Apply[A any, B any](
	a Result[A],
	f Result[func(a A) B],
) Result[B]

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 Error

func Error[V any](err error) Result[V]

Error returns a Result type that represents an error.

func Map

func Map[A any, B any](
	a Result[A],
	f func(a A) B,
) Result[B]

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

func New[V any](value V, err error) Result[V]

New returns a Result. It is syntax sugar for Result{value, error}. If error is a known constant, use Some or Error instead.

func Some

func Some[V any](value V) Result[V]

Some (a.k.a. "Just") returns a Result that contains a value with a nil error.

func Then

func Then[A any, B any](
	a Result[A],
	f func(a A) Result[B],
) Result[B]

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

func (r Result[V]) Filter(f func(v V) bool) Result[V]

Filter examines a Result. If error is nil and f(value) returns true, returns Some(value), otherwise returns Error(error).

func (Result[V]) Match

func (r Result[V]) Match(f func(v V) bool) bool

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.

func (Result[V]) MustError

func (r Result[V]) MustError()

MustError panics if the Result is not an error.

func (Result[V]) Ok

func (r Result[V]) Ok() bool

Ok returns true if the Result's error is nil.

func (Result[V]) Unpack

func (r Result[V]) Unpack() (V, error)

Result returns a plain (value, error) tuple from a Result.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL