Documentation
¶
Overview ¶
Package option provides helper functions for working with Option types.
Option represents an optional value: every Option is either Some (which contains a value), or None (which does not). Option types are very common in Rust code and provide a type-safe alternative to nil pointers and (T, bool) tuples.
Examples ¶
// Create an Option
someValue := option.Some(42)
noneValue := option.None[int]()
// Check and unwrap
if someValue.IsSome() {
fmt.Println(someValue.Unwrap()) // Output: 42
}
// Chain operations
result := someValue.
Map(func(x int) int { return x * 2 }).
UnwrapOr(0) // Output: 84
Index ¶
- func Contains[T comparable](o Option[T], x T) bool
- func MapOr[T any, U any](o Option[T], defaultSome U, f func(T) U) U
- func MapOrElse[T any, U any](o Option[T], defaultFn func() U, f func(T) U) U
- func SafeAssert[T any, U any](o Option[T]) result.Result[Option[U]]
- func Unzip[T any, U any](p Option[pair.Pair[T, U]]) pair.Pair[Option[T], Option[U]]
- func XSafeAssert[U any](o Option[any]) result.Result[Option[U]]
- type Option
- func And[T any, U any](o Option[T], optb Option[U]) Option[U]
- func AndThen[T any, U any](o Option[T], f func(T) Option[U]) Option[U]
- func AssertOpt[T any](v any) Option[T]
- func BoolAssertOpt[T any](i any, ok bool) Option[T]
- func BoolOpt[T any](v T, ok bool) Option[T]
- func ElemOpt[T any](ptr *T) Option[T]
- func FuzzyAssert[T any, U any](o Option[T]) Option[U]
- func Map[T any, U any](o Option[T], f func(T) U) Option[U]
- func None[T any]() Option[T]
- func PtrOpt[U any, T *U](ptr T) Option[T]
- func RetAnyOpt[T any](v any, err error) Option[any]
- func RetOpt[T any](v T, err error) Option[T]
- func Some[T any](value T) Option[T]
- func XFuzzyAssert[U any](o Option[any]) Option[U]
- func ZeroOpt[T comparable](v T) Option[T]
- func Zip[A any, B any](a Option[A], b Option[B]) Option[pair.Pair[A, B]]
- func ZipWith[T any, U any, R any](some Option[T], other Option[U], f func(T, U) R) Option[R]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](o Option[T], x T) bool
Contains returns `true` if the option is a [`Some`] value containing the given value.
func MapOr ¶
MapOr returns the provided default value (if none), or applies a function to the contained value (if any).
func MapOrElse ¶
MapOrElse computes a default function value (if none), or applies a different function to the contained value (if any).
func SafeAssert ¶
SafeAssert asserts Option[T] as result.Result[Option[U]]. NOTE:
If the assertion fails, return error.
Types ¶
type Option ¶
Option is an alias for core.Option[T]. This allows using option.Option[T] instead of core.Option[T].
Option represents an optional value: every Option is either Some (which is non-none T), or None (which is none). Option types are very common in Rust code, as they have a number of uses:
- Initial values
- Return values for functions that are not defined over their entire input range (partial functions)
- Return value for otherwise reporting simple errors, where None is returned on error
- Optional struct fields
- Optional function arguments
- Nullable pointers
- Swapping things out of difficult situations
func AndThen ¶
AndThen returns [`None`] if the option is [`None`], otherwise calls `f` with the wrapped value.
func BoolAssertOpt ¶
BoolAssertOpt wraps a value as an Option. NOTE:
`ok=true` is wrapped as Some, and `ok=false` is wrapped as None.
func BoolOpt ¶
BoolOpt wraps a value as an Option. NOTE:
`ok=true` is wrapped as Some, and `ok=false` is wrapped as None.
func ElemOpt ¶
ElemOpt wraps a value from pointer. NOTE:
`non-nil pointer` is wrapped as Some, and `nil pointer` is wrapped as None.
func FuzzyAssert ¶
FuzzyAssert asserts Option[T] as Option[U]. NOTE:
If the assertion fails, return none.
func None ¶
None returns a none. NOTE:
Option[T].IsNone() returns true, and Option[T].IsSome() returns false.
func PtrOpt ¶
PtrOpt wraps a pointer value. NOTE:
`non-nil pointer` is wrapped as Some, and `nil pointer` is wrapped as None.
func RetAnyOpt ¶
RetAnyOpt wraps a value as an `Option[any]`. NOTE:
`err != nil` or `value`==nil is wrapped as None, and `err == nil` and `value != nil` is wrapped as Some.
func RetOpt ¶
RetOpt wraps a value as an `Option[T]`. NOTE:
`err != nil` is wrapped as None, and `err == nil` is wrapped as Some.
func Some ¶
Some wraps a non-none value. NOTE:
Option[T].IsSome() returns true. and Option[T].IsNone() returns false.
func XFuzzyAssert ¶
XFuzzyAssert asserts Option[any] as Option[U]. NOTE:
If the assertion fails, return none.
func ZeroOpt ¶
func ZeroOpt[T comparable](v T) Option[T]
ZeroOpt wraps a value as an Option. NOTE:
`non-zero T` is wrapped as Some, and `zero T` is wrapped as None.