Documentation
¶
Overview ¶
Package res implements the Of monad for Go.
Index ¶
- func IfErr[T any](v T, err error) func(func(T), func(error))
- type Of
- func Err[T any](e error) Of[T]
- func ErrIf[T any](cond bool, then T, or error) Of[T]
- func Errn[T any](s string) Of[T]
- func Errw[T any](e error, s string) Of[T]
- func From[T comparable](val T, err error) Of[T]
- func FromAny[T any](val T, err error) Of[T]
- func FromAnyReflect[T any](val T, err error) Of[T]
- func FromOpt[T any](o opt.Of[T], err error) Of[T]
- func FromPtr[T any](val *T, err error) Of[T]
- func Morph[T, U any](r Of[T], f func(T) Of[U]) Of[U]
- func OK[T comparable](v T) Of[T]
- func OKAny[T any](v T) Of[T]
- func OKAnyReflect[T any](v T) Of[T]
- func OKOpt[T any](o opt.Of[T]) Of[T]
- func OKPtr[T any](v *T) Of[T]
- func To[T, U any](r Of[T], tf func(T) U) Of[U]
- func ToPtr[T, U any](r Of[T], tf func(T) *U) Of[U]
- func (r Of[T]) Err() error
- func (r Of[T]) FlatMap(f func(T) Of[T]) Of[T]
- func (r Of[T]) Fold(onValue func(T) T, onFail func() T) T
- func (r Of[T]) Into(d *T) error
- func (r Of[T]) MarshalJSON() ([]byte, error)
- func (r Of[T]) OK() bool
- func (r Of[T]) Opt() opt.Of[T]
- func (r Of[T]) Ptr() *T
- func (r *Of[T]) UnmarshalJSON(ba []byte) error
- func (r Of[T]) Unpack() (T, error)
- func (r Of[T]) Val() T
- func (r Of[T]) Valid() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Of ¶
type Of[T any] struct { // contains filtered or unexported fields }
Of[T] is either an OK value or an 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 FromAnyReflect ¶
FromAnyReflect creates Of[T] using reflection to determine validity. Prefer From or FromAny when possible; reflection is 25–50× slower.
func Morph ¶
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 OKAnyReflect ¶
OKAnyReflect creates Of[T] using reflection to determine validity. Prefer OK or OKAny when possible; reflection is 25–50× slower.
func To ¶
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 ¶
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]) 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]) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Of[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (Of[T]) Valid ¶
Valid reports whether the result is OK. Implements fn.Container.