Documentation ¶
Overview ¶
Package opt implements "optional" values. An Opt can either hold an error or a value. The special error ErrEmpty signifies that the option is empty. Options are not "futures" or "promises" but hold a pre-computed value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmpty = errors.New("empty")
ErrEmpty is a constant error value used to signify when an Opt is empty.
Functions ¶
func Caller ¶ added in v0.4.0
func Caller[T any](f FuncSourceErr[T]) func() Opt[T]
Caller wraps an error-returning function in a function returning an opt. Caller is intended for use with seq.SourceOf and similar situation where you repeatedly call a source function that can error. See also Mapper.
Example ¶
i := 0 ints, tail := seq.SourceOf(opt.Caller(func() (int, error) { i++ if i > 3 { return 0, numTooBigError } return i, nil })).TakeWhile(opt.Ok[int]) // ints = [opt.Of(1), opt.Of(2), opt.Of(3)] // tail wraps numTooBigError
func Mapper ¶ added in v0.4.0
Mapper wraps an error-returning function in a function returning an opt. Mapper is intended for use with slice.Mapping and seq.MappingOf and similar transformations. See also Caller.
Example ¶
strInts := seq.SliceOfArgs("1", "two", "3") ints := seq.MappingOf(strInts, opt.Mapper(strconv.Atoi)). Where(opt.Ok[int]). ToSlice() // ints is [opt.Of(1), opt.Of(3)]
Types ¶
type ErrPanic ¶ added in v0.4.0
type ErrPanic struct {
V any
}
ErrPanic wraps a panic value when Recovering or RecoveringMapper recovers from a panic.
type FuncSourceErr ¶ added in v0.4.0
type Future ¶ added in v0.9.0
type Future[T any] struct { // contains filtered or unexported fields }
Future represents a result that will appear at some point in the future.
func Promise ¶ added in v0.9.0
Promise executes a function in a goroutine and returns a Future that can be used to wait for the result. The exec function *must* ensure that the resolve function is called exactly once. The promise does not have to be resolved by the exec function itself. It is allowed to pass the resolve function to other functions and goroutines.
Calling resolve multiple times will cause a panic. Failing to call resolve will cause Future.Await to hang forever.
If the exec function panics it will be recovered and Future.Await will return an option with an ErrPanic.
func PromiseThen ¶ added in v0.9.0
func PromiseThen[S, T any](first *Future[S], exec func(firstResult Opt[S], resolve func(Opt[T]))) *Future[T]
PromiseThen starts another promise when the result of a future is ready. The chained promise is started whether the first result is an error or not.
func (*Future[T]) Await ¶ added in v0.9.0
Await blocks until the result is ready and returns it. If the result is already available the function returns immediately. It is valid to call from any goroutine and as many times as you like.
type Opt ¶
type Opt[T any] struct { // contains filtered or unexported fields }
Opt is a light wrapper around a value or an error. Opts should always be passed by value. If you see a pointer to an Opt anywhere something is wrong.
The zero Opt structure holds the default value for T and no error, and is *not* considered empty.
func Empty ¶
Empty creates a new empty opt. An empty opt stores the special error ErrEmpty and will respond true to Opt.Empty() and false to Opt.Ok.
func Map ¶
Map converts an option into some other type. If you want to keep the same type it may be easier to use Opt.Map. If the opt is empty or an error the mapping function will not be called.
func Recovering ¶ added in v0.9.2
func Recovering[T any](f FuncSourceErr[T]) (op Opt[T])
Recovering a function returning an opt with the result. If the function panics it is recovered and returned as ErrPanic.
func Returning ¶ added in v0.4.0
Returning creates an option from a value and an error.
Example:
fileOpt := opt.Returning(os.Open("/tmp/README.txt"))
func (Opt[T]) Map ¶
Map applies a function to the value of the Opt, unless the Opt is empty. If you need to change the type inside the Opt you will have to use Map.
func (Opt[T]) Must ¶
func (o Opt[T]) Must() T
Must returns the value wrapped by this opt or panics if there isn't one. It should be considered a last resort to use this function. It is almost always better to use Or, OnErr, or Return instead.
func (Opt[T]) Ok ¶
Ok returns true if there is no error associated with this opt. It is guaranteed to be valid to call Opt.Must() if Opt.Ok() returns true.
func (Opt[T]) OnErr ¶
OnErr calls a function if the opt is an error or returns the value directly. The function returns a default value that will be returned from OnErr.