Documentation
¶
Overview ¶
Package option provides a generic implementation of the Option type, similar to Rust's Option type. It is used to encapsulate an optional value: a value that may either contain a value of type T or may be empty.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
Nil = errors.New("nil value in Option")
)
Nil is a pre-defined error to represent a nil value being incorrectly used to create an Option.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option[T any] interface { Cause() error // Cause returns an error if the option is none due to an error. Ok() T // Ok returns the value if present, otherwise a zero value. IsSome() bool // IsSome returns true if the option contains a value. IsNone() bool // IsNone returns true if the option does not contain a value. }
Option is a generic interface that defines methods for handling optional values.
func Flatten ¶
Flatten converts an Option[Option[T]] into a single Option[T]. If the outer Option is none, it returns a none Option with the same error; otherwise, it returns the inner Option.
func Map ¶
Map applies a function to the value within the Option, returning a new Option with the result. If the original Option is none, the function is not executed and a new none Option with the original error is returned.
func None ¶
None creates an Option that contains no value but an error. If no specific error is provided, it defaults to using Nil.