Documentation ¶
Overview ¶
Package opt provides the generic Maybe type for representing optional values without pointers.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Maybe ¶
type Maybe[V any] struct { // contains filtered or unexported fields }
Maybe is a simple implementation of an optional value type.
func FromPtr ¶
FromPtr returns a Maybe that has a defined value of *ptr if ptr is non-nil, or no value if ptr is nil.
func (Maybe[V]) AsPtr ¶
func (m Maybe[V]) AsPtr() *V
AsPtr returns a pointer to the value if the value is defined, or nil otherwise.
func (Maybe[V]) MarshalJSON ¶
MarshalJSON produces whatever JSON representation would normally be produced for the value if a value is defined, or a JSON null otherwise.
func (Maybe[V]) OrElse ¶
func (m Maybe[V]) OrElse(valueIfUndefined V) V
OrElse returns the value of the Maybe if any, or the valueIfUndefined otherwise.
func (Maybe[V]) String ¶
String returns a string representation of the value, or "[none]" if undefined. The string representation of a value is either its own String() if it has such a method, or else the result of fmt.Sprintf with "%v".
func (*Maybe[V]) UnmarshalJSON ¶
UnmarshalJSON sets the Maybe to None[V] if the data is a JSON null, or else unmarshals a value of type V as usual and sets the Maybe to Some(value).