Documentation
¶
Index ¶
- func Must[T any](v T, err error) T
- func MustZero[T comparable](value T)
- func Try(f func() error, err *error)
- type Opt
- func (o Opt[T]) All() iter.Seq[T]
- func (o Opt[T]) IsNone() bool
- func (o Opt[T]) IsSome() bool
- func (o Opt[T]) IsZero() bool
- func (o Opt[T]) MarshalJSON() ([]byte, error)
- func (o *Opt[T]) UnmarshalJSON(buf []byte) error
- func (o Opt[T]) Unwrap() T
- func (o Opt[T]) UnwrapOr(v T) T
- func (o Opt[T]) UnwrapOrElse(fn func() T) T
- type Ptr
- func (o Ptr[T]) All() iter.Seq[T]
- func (o Ptr[T]) IsNone() bool
- func (o Ptr[T]) IsSome() bool
- func (o Ptr[T]) IsZero() bool
- func (o Ptr[T]) MarshalJSON() ([]byte, error)
- func (o *Ptr[T]) UnmarshalJSON(buf []byte) error
- func (o Ptr[T]) Unwrap() T
- func (o Ptr[T]) UnwrapOr(v T) T
- func (o Ptr[T]) UnwrapOrElse(fn func() T) T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Must ¶
Must asserts that the tupel of (T,error) does not contain an error and otherwise panics.
Example ¶
Output: 3
func MustZero ¶
func MustZero[T comparable](value T)
MustZero panics if the given value is not equal to the generic zero. This is also useful to assert a nil error.
Example ¶
MustZero(HelloError(false))
func Try ¶
Try is best used with defer and an according function pointer, which is evaluated when the defer runs the try. It works best with io.Closer. Hopefully, this will get solved at the language level one day.
Example ¶
fn := func() (err error) {
defer Try(Close, &err)
return nil
}
Output: close failed
Types ¶
type Opt ¶
type Opt[T any] struct { // contains filtered or unexported fields }
Opt is introduced because range over func can only represent at most 2 arguments. Processing a (T, ok, error) becomes impossible. Also, it is not correct to always use pointers for modelling or to use hidden error types for clear optional cases where an absent thing is never an error by definition. This also helps for performance edge cases, where you can technically express that a value is really just a value and does not escape.
It sports also a non-nesting custom JSON serialization, which just uses NULL as representation. Note that if T is a pointer type, the Option becomes invalid after unmarshalling because a valid nil pointer cannot be distinguished from an invalid nil pointer in JSON, but you likely should not model your domain that way anyway.
If you already have a pointer, just use its zero value which is nil and not Option. The zero value is safe to use.
func (Opt[T]) MarshalJSON ¶
func (*Opt[T]) UnmarshalJSON ¶
func (Opt[T]) Unwrap ¶
func (o Opt[T]) Unwrap() T
Unwrap makes the assertion that the Option is valid and otherwise panics. Such panic is always a programming error.
func (Opt[T]) UnwrapOr ¶
func (o Opt[T]) UnwrapOr(v T) T
UnwrapOr returns either the contained value or the eagly evaluated given value. See also Opt.UnwrapOrElse.
func (Opt[T]) UnwrapOrElse ¶
func (o Opt[T]) UnwrapOrElse(fn func() T) T
UnwrapOrElse returns either the contained value or evaluates the given func to return an alternative.
type Ptr ¶
type Ptr[T any] struct { // contains filtered or unexported fields }
Ptr is an immutable wrapper type around a pointer to a value of T. It returns always the T and never the pointer to T to express immutability. This is similar to Opt[T] but has an entire different memory characteristic. The optional implementation uses a flag and value type of T which requires the additional bool and the entire T. If you have a lot of options, e.g. in a nested struct, this will cause a lot of waste, but usually it lands on the stack.
This Ptr implementation only keeps a pointer (usually 8 byte) to the heap escaped value but once given to the caller, the wrapped pointer cannot be mutated and only access to the dereferenced value is possible which creates a copy of T.
func (Ptr[T]) MarshalJSON ¶
func (*Ptr[T]) UnmarshalJSON ¶
func (Ptr[T]) Unwrap ¶
func (o Ptr[T]) Unwrap() T
Unwrap makes the assertion that the Option is valid and otherwise panics. Such panic is always a programming error.
func (Ptr[T]) UnwrapOr ¶
func (o Ptr[T]) UnwrapOr(v T) T
UnwrapOr returns either the contained value or the eagly evaluated given value. See also Opt.UnwrapOrElse.
func (Ptr[T]) UnwrapOrElse ¶
func (o Ptr[T]) UnwrapOrElse(fn func() T) T
UnwrapOrElse returns either the contained value or evaluates the given func to return an alternative.