optional

package
v0.0.0-...-aca9518 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 2 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

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

A value that may not be present.

Wrapping optional values with this struct is preferred over pointers for better type safety, avoid accidental mutation and self-explanatory explicitness.

An Optional value is always either `Some(value)` or `None`. When it is `None`, the internal `value` field is always the zero value.

func FromPtr

func FromPtr[T any](ptr *T) Optional[T]

Dereferences a pointer if it is non nil. Converts from the K8s pointer-optional convention.

func GetMap

func GetMap[K comparable, V any](map_ map[K]V, key K) Optional[V]

Returns `Some(map_[key])` if `key` exists in `map_`, None otherwise.

func GetSlice

func GetSlice[T any](slice []T, index int) Optional[T]

Returns `Some(slice[index])` if it exists, None otherwise.

func Map

func Map[T any, U any](v Optional[T], fn func(T) U) Optional[U]

func None

func None[T any]() Optional[T]

Returns an `Optional` representing an absent value.

func Some

func Some[T any](value T) Optional[T]

Returns an `Optional` representing a present value.

func (Optional[T]) Get

func (v Optional[T]) Get() (T, bool)

Converts from `Optional` form to the (value, isPresent) form, which is consistent with Go syntax with map access and type cast.

func (Optional[T]) GetOr

func (v Optional[T]) GetOr(value T) T

Gets the value or falls back to a default value.

func (Optional[T]) GetOrFn

func (v Optional[T]) GetOrFn(fn func() T) T

Gets the value or falls back to a lazily computed value.

func (Optional[T]) GetOrZero

func (v Optional[T]) GetOrZero() T

Gets the value or falls back to the zero value.

func (Optional[T]) GetValueRef

func (v Optional[T]) GetValueRef() *T

Returns a pointer to the value field, which may represent an uninitialized object. Never returns nil, not even when present.

func (Optional[T]) GetValueRefOrNil

func (v Optional[T]) GetValueRefOrNil() *T

Returns a pointer to the value field, ONLY IF the value is present.

func (Optional[T]) GoString

func (v Optional[T]) GoString() string

func (Optional[T]) IsNone

func (v Optional[T]) IsNone() bool

Checks whether the optional is a `None` instead of a `Some`.

func (Optional[T]) IsNoneOr

func (v Optional[T]) IsNoneOr(fn func(T) bool) bool

Returns true if the value is absent or if the present value matches the predicate.

func (Optional[T]) IsSome

func (v Optional[T]) IsSome() bool

Checks whether the optional is a `Some` instead of a `None`.

func (Optional[T]) IsSomeAnd

func (v Optional[T]) IsSomeAnd(fn func(T) bool) bool

Returns true only if the value is present and matches the predicate.

func (Optional[T]) MustGet

func (v Optional[T]) MustGet(msg string) T

Asserts that the receiver is `Some`, panics with the given justification otherwise.

The message should be a justification stating why the value must exist, e.g. "1 + 1 should be 2".

func (Optional[T]) OrFn

func (v Optional[T]) OrFn(fn func() Optional[T]) Optional[T]

Attempt to replace the value with another lazily computed optional.

func (*Optional[T]) SetOrChoose

func (v *Optional[T]) SetOrChoose(value T, preferRight func(T, T) bool)

Sets the receiver to `Some(value)` if it is None or the new value wins `preferRight`.

func (*Optional[T]) SetOrFn

func (v *Optional[T]) SetOrFn(value T, fn func(base, increment T) T)

Initializes the receiver with `value` or updates it with the reduction function `fn`.

Jump to

Keyboard shortcuts

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