res

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package res implements the Of monad for Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IfErr

func IfErr[T any](v T, err error) func(func(T), func(error))

IfErr dispatches between two handlers based on whether err is nil. Calls f_ok(v) if err == nil, f_err(err) otherwise.

Types

type Of

type Of[T any] struct {
	// contains filtered or unexported fields
}

Of[T] is either an OK value or an error.

func Err

func Err[T any](e error) Of[T]

Err creates Of[T] from an error.

func ErrIf

func ErrIf[T any](cond bool, then T, or error) Of[T]

ErrIf returns Of[T] with the value if cond is true, otherwise wraps the error.

func Errn

func Errn[T any](s string) Of[T]

Errn creates Of[T] from an error string.

func Errw

func Errw[T any](e error, s string) Of[T]

Errw creates Of[T] from a wrapped error.

func From

func From[T comparable](val T, err error) Of[T]

From creates Of[T] from a standard (T, error) return where T is comparable.

func FromAny

func FromAny[T any](val T, err error) Of[T]

FromAny creates Of[T] from a standard (T, error) return, bypassing zero checks.

func FromAnyReflect

func FromAnyReflect[T any](val T, err error) Of[T]

FromAnyReflect creates Of[T] using reflection to determine validity. Prefer From or FromAny when possible; reflection is 25–50× slower.

func FromOpt

func FromOpt[T any](o opt.Of[T], err error) Of[T]

FromOpt creates Of[T] from an existing option and error.

func FromPtr

func FromPtr[T any](val *T, err error) Of[T]

FromPtr creates Of[T] from a standard (*T, error) return.

func Morph

func Morph[T, U any](r Of[T], f func(T) Of[U]) Of[U]

Morph maps Of[T] to Of[U] using f, which returns a Of[U]. If the result has an error, f is not called and the error propagates.

func OK

func OK[T comparable](v T) Of[T]

OK creates Of[T] from a comparable value. Invalid if value is zero.

func OKAny

func OKAny[T any](v T) Of[T]

OKAny creates Of[T] from any value, bypassing zero checks.

func OKAnyReflect

func OKAnyReflect[T any](v T) Of[T]

OKAnyReflect creates Of[T] using reflection to determine validity. Prefer OK or OKAny when possible; reflection is 25–50× slower.

func OKOpt

func OKOpt[T any](o opt.Of[T]) Of[T]

OKOpt creates Of[T] from an existing option.

func OKPtr

func OKPtr[T any](v *T) Of[T]

OKPtr creates Of[T] from a pointer. Returns an error if the pointer is nil.

func To

func To[T, U any](r Of[T], tf func(T) U) Of[U]

To maps Of[T] to Of[U] by applying tf to the value. If the result has an error, tf is not called and the error propagates.

func ToPtr

func ToPtr[T, U any](r Of[T], tf func(T) *U) Of[U]

ToPtr maps Of[T] to Of[U] by applying tf and unwrapping the pointer. If the result has an error, tf is not called and the error propagates.

func (Of[T]) Err

func (r Of[T]) Err() error

Err returns the underlying error, or nil if OK.

func (Of[T]) FlatMap

func (r Of[T]) FlatMap(f func(T) Of[T]) Of[T]

FlatMap applies f to the value if OK, propagating the error otherwise.

func (Of[T]) Fold

func (r Of[T]) Fold(onValue func(T) T, onFail func() T) T

Fold returns onValue(t) if OK, otherwise onFail().

func (Of[T]) Into

func (r Of[T]) Into(d *T) error

Into writes the value to *d and returns the error.

func (Of[T]) MarshalJSON

func (r Of[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Of[T]) OK

func (r Of[T]) OK() bool

OK reports whether the result has no error and a valid option.

func (Of[T]) Opt

func (r Of[T]) Opt() opt.Of[T]

Opt returns the underlying Option[T].

func (Of[T]) Ptr

func (r Of[T]) Ptr() *T

Ptr returns a pointer to the value, or nil if not OK.

func (*Of[T]) UnmarshalJSON

func (r *Of[T]) UnmarshalJSON(ba []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (Of[T]) Unpack

func (r Of[T]) Unpack() (T, error)

Unpack returns the value and error.

func (Of[T]) Val

func (r Of[T]) Val() T

Val returns the underlying value. Zero if not OK.

func (Of[T]) Valid

func (r Of[T]) Valid() bool

Valid reports whether the result is OK. Implements fn.Container.

Jump to

Keyboard shortcuts

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