Documentation
¶
Overview ¶
Package option provides types and functions to work with optional values.
Index ¶
- Variables
- type Any
- type Basic
- func (b Basic[T]) Call(fn func(T))
- func (b Basic[T]) Convert(fn func(T) T) (_ Basic[T])
- func (b Basic[T]) Get() (_ T, _ bool)
- func (b Basic[T]) IsOk() bool
- func (b Basic[T]) KeepOkIf(fn func(T) bool) (_ Basic[T])
- func (b Basic[T]) MustGet() T
- func (b Basic[T]) Or(t T) T
- func (b Basic[T]) OrCall(fn func() T) (_ T)
- func (b Basic[T]) OrEmpty() (_ T)
- func (b Basic[T]) OrFalse() (_ T)
- func (b Basic[T]) OrZero() (_ T)
- func (b Basic[T]) ToAny(fn func(T) any) (_ Basic[any])
- func (b Basic[T]) ToBool(fn func(T) bool) (_ Basic[bool])
- func (b Basic[T]) ToByte(fn func(T) byte) (_ Basic[byte])
- func (b Basic[T]) ToError(fn func(T) error) (_ Basic[error])
- func (b Basic[T]) ToInt(fn func(T) int) (_ Basic[int])
- func (b Basic[T]) ToNotOkIf(fn func(T) bool) (_ Basic[T])
- func (b Basic[T]) ToOpt() (_ *T)
- func (b Basic[T]) ToRune(fn func(T) rune) (_ Basic[rune])
- func (b Basic[T]) ToString(fn func(T) string) (_ Basic[string])
- type Bool
- type Byte
- type Error
- type Int
- type Rune
- type String
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Basic ¶
type Basic[T any] struct { // contains filtered or unexported fields }
Basic represents an optional value of type T.
func IfNotNil ¶ added in v0.10.0
IfNotNil returns an ok option of *what t points at* provided that t is not nil, or not-ok otherwise. It converts a pointer-based pseudo-option (where nil means absent) into a formal option.
func IfNotZero ¶ added in v0.7.0
func IfNotZero[T comparable](t T) (_ Basic[T])
IfNotZero returns an ok option of t provided that t is not the zero value for T, or not-ok otherwise.
func (Basic[T]) Call ¶
func (b Basic[T]) Call(fn func(T))
Call applies fn to the option's value provided that the option is ok.
func (Basic[T]) Convert ¶ added in v0.9.0
Convert returns the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Basic[T]) Get ¶
Get returns the option's value and a boolean indicating the option's status. It unpacks the option's fields into Go's comma-ok idiom, making it useful in the usual Go conditional constructs. When used in this manner, myVal doesn't stick around in the namespace when you're done with it:
if myVal, ok := o.Get; ok {
do some stuff
}
func (Basic[T]) KeepOkIf ¶
KeepOkIf returns b provided that applying fn to an ok option's value returns true, or the original option otherwise. It is the filter operation. Since Go doesn't offer a convenient lambda syntax for constructing the negation of a function's output, there is a ToNotOkIf method as well.
func (Basic[T]) MustGet ¶
func (b Basic[T]) MustGet() T
MustGet returns the option's value or panics if the option is not ok.
func (Basic[T]) Or ¶
func (b Basic[T]) Or(t T) T
Or returns the option's value provided that the option is ok, otherwise t.
func (Basic[T]) OrCall ¶
func (b Basic[T]) OrCall(fn func() T) (_ T)
OrCall returns the option's value provided that it is ok, otherwise the result of calling fn.
func (Basic[T]) OrEmpty ¶
func (b Basic[T]) OrEmpty() (_ T)
OrEmpty returns the option's value provided that it is ok, otherwise the zero value for T. It is a more readable alias for OrZero when T is string.
func (Basic[T]) OrFalse ¶
func (b Basic[T]) OrFalse() (_ T)
OrFalse returns the option's value provided that it is ok, otherwise the zero value for T. It is a more readable alias for OrZero when T is bool.
func (Basic[T]) OrZero ¶
func (b Basic[T]) OrZero() (_ T)
OrZero returns the option's value provided that it is ok, otherwise the zero value for T. See OrEmpty and OrFalse for more readable aliases of OrZero when T is string or bool.
func (Basic[T]) ToAny ¶
ToAny returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Basic[T]) ToBool ¶
ToBool returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Basic[T]) ToByte ¶
ToByte returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Basic[T]) ToError ¶
ToError returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Basic[T]) ToInt ¶
ToInt returns an option of the result of applying fn to the option's value provided that the option is ok, or not-ok otherwise.
func (Basic[T]) ToNotOkIf ¶
ToNotOkIf returns a not-ok option provided that applying fn to an ok option's value returns true, or the original option otherwise. It is the filter operation with negation. Since Go doesn't offer a convenient lambda syntax for constructing the negation of a function's output, having negation built-in is both a convenience and keeps consuming code readable.
func (Basic[T]) ToOpt ¶
func (b Basic[T]) ToOpt() (_ *T)
ToOpt returns a pointer-based pseudo-option of the pointed-at value provided that the option is ok, or not-ok otherwise. By convention, in consuming code, we suffix a pseudo-option's variable name with an "Opt" suffix to clarify the pointer's meaning and use, hence "ToOpt".