result

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package result implements a R{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 R result type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Applicator added in v2.2.0

func Applicator[X any, Y any](
	f R[func(x X) Y],
) func(x X) R[Y]

Applicator turns function "R[f]: X => Y" into "f: X => R[Y]".

func Compose added in v2.2.0

func Compose[X any, Y any, Z any](
	xy func(R[X]) R[Y],
	yz func(R[Y]) R[Z],
) func(R[X]) R[Z]

Compose takes two functions of the form "xy: R[X] => R[Y]" and "yz: R[Y] => R[Z]" and returns a function "xz(R[X]) => R[Z]".

func FlatMap added in v2.2.0

func FlatMap[X any, Y any](
	f func(x X) R[Y],
) func(x2 R[X]) R[Y]

FlatMap turns function "f: X => R[Y]" into "f: R[X] => R[Y]".

func Lift

func Lift[X any, Y any](
	f func(x X) Y,
) func(x X) R[Y]

Lift converts a function of the form "f: X => Y" to the form "f: X => R[Y]" where R[Y] == Some(y).

func Map

func Map[X any, Y any](
	f func(x X) Y,
) func(x2 R[X]) R[Y]

Map turns function "f: X => Y" into "f: R(X) => R[Y]".

func UnwrapFunc

func UnwrapFunc[X any, Y any](
	f func(x X) R[Y],
) func(x X) (Y, error)

UnwrapFunc converts a function of the form "f: X => R[Y]" to the form "f: X => (Y, error)".

func WrapFunc

func WrapFunc[X any, Y any](
	f func(x X) (Y, error),
) func(x X) R[Y]

WrapFunc converts a function of the form "f: X => (Y, error)" to the form "f: X => R[X].

Types

type R added in v2.2.0

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

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

func Error

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

Error returns a R that is an error.

func New

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

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

func Some

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

Some returns a R that contains a value and is not an error.

func (R[V]) Else added in v2.2.0

func (r R[V]) Else(v V) V

Else returns R.value if not an error, otherwise returns the provided argument instead.

func (R[V]) JoinError added in v2.7.0

func (r R[V]) JoinError(err error) R[V]

JoinError returns a new Error based on an existing R. If the existing R is not an error, returns Error(err). Otherwise, returns Error(errors.Join(existingError, err)).

func (R[V]) Must added in v2.2.0

func (r R[V]) Must() V

Must returns a R's value. If the R is an error, panics.

func (R[V]) MustError added in v2.2.0

func (r R[V]) MustError() error

MustError returns a R's error. Panics if the R is not an error.

func (R[V]) Success added in v2.2.0

func (r R[V]) Success() bool

Success returns true if the R is not an error.

func (R[V]) Unpack added in v2.2.0

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

Unpack returns a plain (value, error) tuple from a R.

Jump to

Keyboard shortcuts

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