Documentation
¶
Overview ¶
Package optval provides a way to represent optional values in Go.
Index ¶
- func Collect[T any](values ...Value[T]) []T
- func Compare[T cmp.Ordered](a, b Value[T]) int
- func IsNone[T any](v Value[T]) bool
- func IsSome[T any](v Value[T]) bool
- func Less[T cmp.Ordered](a, b Value[T]) bool
- func OrZero[T any](v Value[T]) T
- func Unwrap[T any](v Value[T]) (T, bool)
- func UnwrapFilter[T any](values iter.Seq[Value[T]]) iter.Seq[T]
- type Value
- func ByKey[K comparable, V any, M ~map[K]V](key K, m M) Value[V]
- func Filter[T any, F ~func(T) bool](v Value[T], f F) Value[T]
- func FindSome[T any](values iter.Seq[Value[T]]) Value[T]
- func FlatMap[T, U any, F ~func(T) Value[U]](v Value[T], f F) Value[U]
- func Flatten[T any](v Value[Value[T]]) Value[T]
- func FromPtr[T any](value *T) Value[T]
- func Key[K comparable, V any, M ~map[K]V](key K, m M) Value[K]
- func Map[T, U any, F ~func(T) U](v Value[T], f F) Value[U]
- func MapFromPtr[T, U any, F ~func(T) U](v *T, f F) Value[U]
- func New[T any](value T, valid bool) Value[T]
- func None[T any]() Value[T]
- func Or[T any](values ...Value[T]) Value[T]
- func Some[T any](value T) Value[T]
- func (v Value[T]) CopyPtr() *T
- func (v Value[T]) IsNone() bool
- func (v Value[T]) IsSome() bool
- func (v Value[T]) Or(other Value[T]) Value[T]
- func (v Value[T]) OrElse(value func() Value[T]) Value[T]
- func (v Value[T]) OrElseSome(value func() T) T
- func (v Value[T]) OrSome(value T) T
- func (v Value[T]) OrZero() T
- func (v *Value[T]) Replace(value T) Value[T]
- func (v *Value[T]) Reset()
- func (v *Value[T]) Take() Value[T]
- func (v Value[T]) Unwrap() (T, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collect ¶
Collect collects all values from the given optional values and returns them as a slice.
func Less ¶
Less returns true if the first value is less than the second one. Some is considered less than None.
func OrZero ¶ added in v0.3.0
OrZero returns the inner value of the optional value v if it is Some, otherwise it returns zero value.
Types ¶
type Value ¶
type Value[T any] struct { // contains filtered or unexported fields }
Value represents an optional value of type T. It can be either Some with inner value or None.
func ByKey ¶
func ByKey[K comparable, V any, M ~map[K]V](key K, m M) Value[V]
ByKey returns Some value in case it is found in the provided map by the provided key.
func Filter ¶
Filter filters optional Value by the given predicate. If the value is Some and the predicate returns true, it returns the same value. Otherwise, it returns None.
func FindSome ¶ added in v0.3.0
FindSome returns the first optional value that is Some or None if all values are None. It returns None if the sequence is empty.
func FromPtr ¶
FromPtr returns Some with a copy of the given value if the provided pointer is not nil. Otherwise, it returns None.
func Key ¶
func Key[K comparable, V any, M ~map[K]V](key K, m M) Value[K]
Key returns Some key in case it is found in the provided map.
func MapFromPtr ¶
MapFromPtr transforms v to optional Value using the given function. If v is nil, it returns None. Otherwise, it returns Some with the result of the function call.
func Or ¶ added in v0.3.0
Or returns the first optional value that is Some or None if all values are None.
func (Value[T]) CopyPtr ¶
func (v Value[T]) CopyPtr() *T
CopyPtr returns a pointer to a copy of the inner value T if present, otherwise it returns nil.
func (Value[T]) Or ¶
Or returns the optional value v if it is Some, otherwise it returns provided value.
func (Value[T]) OrElse ¶
OrElse returns the optional value v if it is Some, otherwise it calls provided function and returns its result.
func (Value[T]) OrElseSome ¶
func (v Value[T]) OrElseSome(value func() T) T
OrElseSome returns the inner value T if it is Some, otherwise it calls provided function and returns its result.
func (Value[T]) OrSome ¶
func (v Value[T]) OrSome(value T) T
OrSome returns the inner value T if present, otherwise it returns provided value.
func (Value[T]) OrZero ¶
func (v Value[T]) OrZero() T
OrZero returns the inner value T if present, otherwise it returns zero value.
func (*Value[T]) Replace ¶
Replace returns a copy of optional value v and sets it to Some with the given value.