Documentation
¶
Overview ¶
Package optional provides a way to represent optional values.
Index ¶
- func Compare[T cmp.Ordered](a, b Optional[T]) int
- func Equal[T comparable](a, b Optional[T]) bool
- type Optional
- func As[T any, X any](x Optional[X]) Optional[T]
- func AtIndex[E any, S ~[]E](s S, i int) Optional[E]
- func First[E any, S ~[]E](s S) Optional[E]
- func Key[K comparable, V any, M ~map[K]V](m M, k K) Optional[V]
- func Last[E any, S ~[]E](s S) Optional[E]
- func None[T any]() Optional[T]
- func Some[T any](v T) Optional[T]
- func Transform[T, X any](x Optional[X], fn func(X) T) Optional[T]
- func TryTransform[T, X any](x Optional[X], fn func(X) (T, bool)) Optional[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶
Compare returns an integer comparing two optional values.
An absent value is less than a present value.
func Equal ¶
func Equal[T comparable](a, b Optional[T]) bool
Equal returns true if two optional values are equal.
Two absent values are considered equal.
Types ¶
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional represents an optional value of type T.
func As ¶
As applies a type assertion to v. It returns an empty optional value if the type assertion fails.
func Key ¶
func Key[K comparable, V any, M ~map[K]V](m M, k K) Optional[V]
Key returns the value associated with key k in map m, or None if k is not present in m.
func Transform ¶
Transform applies a transformation to v, returning a new optional value that contains the result of the function.
If v's value is not present, the returned optional value will also not contain a value.
func TryTransform ¶
TryTransform applies a transformation to v, returning a new optional value that contains the result of the function.
If v's value is not present, the returned optional value will also not contain a value.
func (Optional[T]) Format ¶
Format implements fmt.Formatter.
func (Optional[T]) Get ¶
func (o Optional[T]) Get() T
Get returns the optional value, or panics if it is not present.