Documentation ¶
Index ¶
- func Equal[T comparable](a, b Option[T]) bool
- type Option
- func Coalesce[T any](os ...Option[T]) Option[T]
- func FromMaybe[T any](v T, ok bool) Option[T]
- func FromPointer[T any](v *T) Option[T]
- func Join[A, B, R any](a Option[A], b Option[B], joinfn func(A, B) R) Option[R]
- func Map[I, O any](in Option[I], mapfn func(I) Option[O]) Option[O]
- func None[T any]() Option[T]
- func Some[T any](v T) Option[T]
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) MaybeUnwrap() (T, bool)
- func (o Option[T]) None() bool
- func (o Option[T]) Some() bool
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- func (o Option[T]) Unwrap() T
- func (o Option[T]) UnwrapOr(v T) T
- func (o Option[T]) UnwrapOrZero() T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
func Equal[T comparable](a, b Option[T]) bool
Equal will compare the value in two options and check if their equal. If both are none, that is interpretted as "equal."
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option represents an optional value. Every Option has either has something or has none. If None() reports true, then calls to Unwrap() will panic. To prevent this, Option[T] should be treated like a pointer and Unwrap() like a dereference.
UnwrapOr() is a handy function to reduce the verbosity of checking an Option[T]'s state. UnwrapOr() will return the contained value if present, OR it will return the value provided.
Option[T] is immutable once created.
The zero-value of Option[T] is safe and will just report None() => true
Inspired by Rust's Option<T>: https://doc.rust-lang.org/std/option/index.html
func Coalesce ¶
Coalesce will take 0 or more Option and will return the first one that is Some value.
func FromMaybe ¶
FromMaybe will take a tuple of a value and a bool representing the value's status and convert it to an Option. If true, a Some[T] is returned, otherwise None[T] is returned.
func FromPointer ¶
FromPointer will take in a pointer to a value and dereference it if it is not nil and return a Some[T](), if it is nil it will return None[T]().
func Join ¶
Join allows for two Options to be used to create a new value if they are both present. If either is not present, then a None[R] will be returned.
func Map ¶
Map allows a function to be run on the present value of an option if it is actually present and then optionally return something else from that value.
func (Option[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler
func (Option[T]) MaybeUnwrap ¶
MaybeUnwrap allows the underlying value to be retrieved in a more idiomatic way by returning a tuple of the possible underlying value and a bool that will be `true` if the value was present. The returned value will be the zeroed value of T if it is not present.
func (Option[T]) None ¶
None is just the opposite of Some(). True means Unwrap() panicks. False means Unwrap() will not panic.
func (Option[T]) Some ¶
Some reports whether there is a value contained or not. A returned value of `true` means there is a value and it can be retrieved with Unwrap() without it panicking. A returned value of `false` means there is no value and any call to Unwrap() will cause a panic.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler
func (Option[T]) Unwrap ¶
func (o Option[T]) Unwrap() T
Unwrap retrieves the underlying value if there is one. Unwrap WILL PANIC if there is no value.
func (Option[T]) UnwrapOr ¶
func (o Option[T]) UnwrapOr(v T) T
UnwrapOr is a safer version of Unwrap() that will return the provided fallback value if the Option[T] does not contain a value.
func (Option[T]) UnwrapOrZero ¶
func (o Option[T]) UnwrapOrZero() T
UnwrapOrZero allows the either the underlying value or a zeroed value of T to be returned. If the value is present it will be returned, otherwise a zero value will be returned.