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 ¶
- Variables
- func IsNone[T any](opt Option[T]) bool
- func IsSome[T any](opt Option[T]) bool
- func SetJsonCoder(coder JsonCoder)
- func WrapFn[T any](fn func() (T, error)) func() Option[T]
- type JsonCoder
- type Option
- func Flatten[T any](opt Option[Option[T]]) Option[T]
- func Map[T, U any](opt Option[T], fn func(T) U) Option[U]
- func None[T any](causes ...error) Option[T]
- func Process[T, U any](opt Option[T], fn func(T) (U, error)) Option[U]
- func Some[T any](t T) Option[T]
- func Wrap[T any](val T, errs ...error) Option[T]
Constants ¶
This section is empty.
Variables ¶
var (
Nil = errors.New("nil value")
)
Nil is a pre-defined error to represent a nil value being incorrectly used to create an Option.
Functions ¶
func IsNone ¶ added in v1.0.1
IsNone returns true if the option does not hold a value (i.e., cause is not nil).
func SetJsonCoder ¶ added in v1.1.2
func SetJsonCoder(coder JsonCoder)
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. }
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.
func Process ¶
Process 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.