import "github.com/dashjay/xiter/pkg/optional"Package optional provides a type which can be used to represent a value that may or may not be present like option in Rust.
type O
type O[T any] struct {
// contains filtered or unexported fields
}func Empty
func Empty[T any]() O[T]Empty creates an empty Optional with no value.
func FromValue
func FromValue[T any](v T) O[T]FromValue creates an Optional from a value.
func FromValue2
func FromValue2[T any](v T, ok bool) O[T]FromValue2 creates an Optional from a value and a boolean meaning whether the value is valid.
HINT:
if we have a function defined as fn () (T, bool),
we can use FromValue2(fn()) instead of
if v, ok := fn(); ok { FromValue(v) } else { Empty[T]() }
func (O[T]) Must
func (o O[T]) Must() TMust directly return the value of the Optional.
❌WARNING: Panic if the Optional has no value.
func (O[T]) Ok
func (o O[T]) Ok() boolOk returns whether the Optional has a valid value.
func (O[T]) Ptr
func (o O[T]) Ptr() *TPtr returns a pointer to the value of the Optional. return nil if the Optional has no value.
func (O[T]) ValueOr
func (o O[T]) ValueOr(dft T) TValueOr returns the value of the Optional if it has a valid value, otherwise returns the given default value.
func (O[T]) ValueOrZero
func (o O[T]) ValueOrZero() TValueOrZero returns the value of the Optional if it has a valid value, otherwise returns the zero value of T.
Generated by gomarkdoc