Documentation
¶
Overview ¶
Package opt defines types for working with opt values, inspired by Java's `Optional` class. An opt value either contains a value of a certain type (Some), or it is empty (None). This package provides a way to safely handle such values without the risk of null ptr dereferences. It includes functions for creating opt values, checking if they are present, and retrieving the value if it is present.
Index ¶
- type None
- func (n None[V]) Filter(_ func(V) bool) Optional[V]
- func (n None[V]) Get() (V, bool)
- func (n None[V]) GetOrDefault(defaultValue V) V
- func (n None[V]) GetOrFunc(f func() V) V
- func (n None[V]) GetOrZero() V
- func (n None[V]) IfPresent(_ func(V)) bool
- func (n None[V]) IfPresentElse(_ func(V), f func()) bool
- func (n None[V]) Present() bool
- func (n None[V]) String() string
- func (n None[V]) Tap(_ func(V)) Optional[V]
- type Optional
- func Any[V any](options ...Optional[V]) Optional[V]
- func Empty[V any]() Optional[V]
- func Map[V, U any](o Optional[V], mapper func(V) U) Optional[U]
- func Maybe[V any](value V, ok bool) Optional[V]
- func MaybeMap[V, U any](o Optional[V], mapper func(V) (U, bool)) Optional[U]
- func Of[V any](value V) Optional[V]
- func OptionalMap[V, U any](o Optional[V], mapper func(V) Optional[U]) Optional[U]
- type Some
- func (s Some[V]) Filter(f func(V) bool) Optional[V]
- func (s Some[V]) Get() (V, bool)
- func (s Some[V]) GetOrDefault(_ V) V
- func (s Some[V]) GetOrFunc(_ func() V) V
- func (s Some[V]) GetOrZero() V
- func (s Some[V]) IfPresent(f func(V)) bool
- func (s Some[V]) IfPresentElse(f func(V), _ func()) bool
- func (s Some[V]) Present() bool
- func (s Some[V]) String() string
- func (s Some[V]) Tap(f func(V)) Optional[V]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type None ¶
type None[V any] struct { }
None represents the absence of a value.
func (None[V]) GetOrDefault ¶
func (n None[V]) GetOrDefault(defaultValue V) V
func (None[V]) IfPresentElse ¶
type Optional ¶
type Optional[V any] interface { fmt.Stringer // Present returns true if the Optional contains a value, false otherwise. Present() bool // Get returns the value contained in the Optional and an indicator of whether the Optional is empty. // If the Optional is empty, the value returned is the zero value of type V. Get() (V, bool) // GetOrZero returns the value contained in the Optional, or the zero value of type V if the Optional is empty. GetOrZero() V // GetOrDefault returns the value contained in the Optional, or the provided default value if the Optional is empty. GetOrDefault(defaultValue V) V // GetOrFunc returns the value contained in the Optional, or the result of calling the provided function if the Optional is empty. GetOrFunc(func() V) V // IfPresent calls the provided function with the value contained in the Optional if the Optional is not empty. // Returns true if the function was called, false otherwise. IfPresent(func(V)) bool // IfPresentElse calls the first function with the value contained in the Optional if the Optional is not empty, or the second function otherwise. // Returns true if the first function was called, false otherwise. IfPresentElse(func(V), func()) bool // Filter returns an Optional containing the value contained in the Optional if the provided predicate returns true for that value. // If the Optional is empty, an empty Optional is returned. Filter(func(V) bool) Optional[V] // Tap calls the provided function with the value contained in the Optional if the Optional is not empty. // Returns the Optional itself for chaining. Tap(func(V)) Optional[V] }
Optional is a generic type that takes one type parameter V and represents a value that may or may not be present. It is similar to Java's Optional type.
func Any ¶
Any returns the first non-empty Optional from the provided options. If all options are empty, an empty Optional is returned.
func Map ¶
Map returns an Optional containing the result of applying the provided mapper function to the value contained in the provided Optional. If the provided Optional is empty, an empty Optional is returned; otherwise, a non-empty Optional is returned.
Example usage:
o := opt.Map( opt.Of(1), func(i int) string { return fmt.Sprintf("%d", i) }, ) // opt.Some("1") o = opt.Map( opt.Empty[int](), func(i int) string { return fmt.Sprintf("%d", i) }, ) // opt.None()
func Maybe ¶
Maybe returns a non-empty Optional wrapping the provided value if ok is true; an empty Optional, otherwise.
func MaybeMap ¶
MaybeMap returns an Optional containing the result of applying the provided mapper function to the value contained in the provided Optional. If the provided Optional is empty, or if the mapper returns false, an empty Optional is returned; otherwise, a non-empty Optional is returned.
Example usage:
o := opt.MaybeMap( opt.Of(1), func(i int) (string, bool) { return fmt.Sprintf("%d", i), true }, ) // opt.Some("1") o = opt.MaybeMap( opt.Of(1), func(i int) (string, bool) { return fmt.Sprintf("%d", i), false }, ) // opt.None() o = opt.MaybeMap( opt.Empty[int](), func(i int) (string, bool) { return fmt.Sprintf("%d", i), true }, ) // opt.None()
func OptionalMap ¶
OptionalMap returns an Optional containing the result of applying the provided mapper function to the value contained in the provided Optional. If the provided Optional is empty, of if the mapper returns an empty Optional, an empty Optional is returned; otherwise, a non-empty Optional is returned.
Example usage:
o := opt.OptionalMap( opt.Of(1), func(i int) opt.Optional[string] { return opt.Of(fmt.Sprintf("%d", i)) }, ) // opt.Some("1") o = opt.OptionalMap( opt.Of(1), func(i int) opt.Optional[string] { return opt.Empty[string]() }, ) // opt.None() o = opt.OptionalMap( opt.Empty[int](), func(i int) opt.Optional[string] { return opt.Of(fmt.Sprintf("%d", i)) }, ) // opt.None()
type Some ¶
type Some[V any] struct { Value V }
Some represents the presence of a value.
func (Some[V]) GetOrDefault ¶
func (s Some[V]) GetOrDefault(_ V) V