Documentation
¶
Overview ¶
Package optional provides a utility type for representing optional values
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 }
Optional represents a value that may or may not be present.
Type Parameters:
- T: The type of the value being wrapped.
Example:
optionalString := Of("hello") if optionalString.IsPresent() { fmt.Println("Value:", optionalString.Value()) // Output: Value: hello } emptyOptional := Empty[int]() fmt.Println("Is present:", emptyOptional.IsPresent()) // Output: Is present: false
func Empty ¶
Empty creates an Optional with no value.
Returns:
- An empty Optional.
Example:
emptyOptional := Empty[string]() // Optional[string] with no value
func Maybe ¶ added in v0.1.0
Maybe transforms an optional value of type T into an optional value of type U. If the provided optional is empty, does not do anything and returns an empty optional. This method should be on the Optional type directly, but Go does not allow type parameters on methods, only on functions.
Parameters
- o: optional value
- fn: function that takes an element of type T and returns an element of type U
Returns:
- An optional of type U
Example:
res := Maybe(Empty[int](), func(x int) int { return x+1 }) // res = Optional (empty)
res := Maybe(Of(1), func(x int) int { return x+1 } // res = Optional (containing value 2)
func New ¶ added in v0.1.0
New creates an Optional containing the provided value. If the provided value is nil, the Optional will be empty.
Parameters:
- v: Pointer to the value to wrap in an Optional.
Returns:
- An Optional containing the provided value.
Example:
n = 42 res := New(&n) // res = Optional containing 42 res := New[int](nil) // res = empty Optional
func Of ¶
Of creates an Optional containing the provided value. The created Optional will not be empty.
Parameters:
- v: The value to wrap in an Optional.
Returns:
- An Optional containing the provided value.
Example:
optionalInt := Of(42) // Optional[int] containing 42
func (Optional[T]) IsEmpty ¶
IsEmpty checks whether the Optional contains a value.
Returns:
- true if the Optional does not contain a value, false otherwise.
Example:
optionalString := Of("hello") fmt.Println(optionalString.IsEmpty()) // Output: false emptyOptional := Empty[string]() fmt.Println(emptyOptional.IsEmpty()) // Output: true
func (Optional[T]) IsPresent ¶
IsPresent checks whether the Optional contains a value.
Returns:
- true if the Optional contains a value, false otherwise.
Example:
optionalString := Of("hello") fmt.Println(optionalString.IsPresent()) // Output: true emptyOptional := Empty[string]() fmt.Println(emptyOptional.IsPresent()) // Output: false
func (Optional[T]) Raw ¶ added in v0.1.0
func (o Optional[T]) Raw() *T
Raw gives access to the pointer wrapped in the Optional The pointer is nil if the Optional is empty. This method is meant to facilitate the use of Optional in systems that handle optional values as pointers (like database storage).
Returns:
- A pointer to the value in this Optional
Example:
res := Empty[int]().Raw() // res = nil res := Of(42).Raw() // res = pointer to the value 42
func (Optional[T]) Value ¶
func (o Optional[T]) Value() T
Value retrieves the value stored in the Optional.
Returns:
- The stored value if present.
- A zero value of type T if the Optional is empty.
Example:
optionalInt := Of(42) fmt.Println(optionalInt.Value()) // Output: 42 emptyOptional := Empty[int]() fmt.Println(emptyOptional.Value()) // Output: 0
func (Optional[T]) ValueOr ¶
func (o Optional[T]) ValueOr(t T) T
ValueOr retrieves the value stored in the Optional.
Parameters:
- t: The value to return if Optional is empty.
Returns:
- The stored value if present.
- t value of type T if the Optional is empty.
Example:
optionalInt := Of(42) fmt.Println(optionalInt.Value()) // Output: 42 emptyOptional := Empty[int]() fmt.Println(emptyOptional.ValueOr(4)) // Output: 4