Documentation
¶
Overview ¶
Package opt provides a generic Optional type that can be used to represent a value that may or may not be present.
Index ¶
- type Optional
- func (o Optional[T]) Filter(filter func(T) bool) Optional[T]
- func (o Optional[T]) Get() T
- func (o Optional[T]) IfPresent(consumer func(T))
- func (o Optional[T]) IfPresentOrElse(consumer func(T), or func())
- func (o Optional[T]) IsEmpty() bool
- func (o Optional[T]) IsPresent() bool
- func (o Optional[T]) MapToAny(mapper func(T) any) Optional[any]
- func (o Optional[T]) Or(supplier func() Optional[T]) Optional[T]
- func (o Optional[T]) OrElse(other T) T
- func (o Optional[T]) OrElseGet(supplier func() T) T
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 }
Optional is a generic type that encapsulates a value that may or may not be present. It provides methods to work with the value safely.
func Map ¶
Map applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped value; otherwise, it returns an empty Optional.
func OfNullable ¶
OfNullable creates an Optional from a pointer to a value. If the pointer is nil, it returns an empty Optional.
func (Optional[T]) Filter ¶
Filter returns an Optional containing the value if it is present and satisfies the provided filter function; otherwise, it returns an empty Optional.
func (Optional[T]) Get ¶
func (o Optional[T]) Get() T
Get retrieves the value contained in the Optional. It panics if the value is not present (i.e., if IsEmpty() returns true).
func (Optional[T]) IfPresent ¶
func (o Optional[T]) IfPresent(consumer func(T))
IfPresent executes the provided consumer function with the value contained in the Optional if present.
func (Optional[T]) IfPresentOrElse ¶
func (o Optional[T]) IfPresentOrElse(consumer func(T), or func())
IfPresentOrElse executes the provided consumer function with the value if present; otherwise, it executes the provided alternative function.
func (Optional[T]) IsEmpty ¶
IsEmpty returns true if the Optional does not contain a value; otherwise, it returns false.
func (Optional[T]) IsPresent ¶
IsPresent returns true if the Optional contains a value; otherwise, it returns false.
func (Optional[T]) MapToAny ¶
MapToAny applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped value; otherwise, it returns an empty Optional.
func (Optional[T]) Or ¶
Or returns the value contained in the Optional if present; otherwise, it invokes the provided supplier function to obtain a new Optional.