Documentation ¶
Overview ¶
Package value defines adapters for value types.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func At ¶ added in v0.6.1
func At[T any](p *T) T
At returns the value pointed to by p, or zero if p == nil.
func AtDefault ¶ added in v0.6.1
func AtDefault[T any](p *T, dflt T) T
AtDefault returns the value pointed to by p, or dflt if p == nil.
Types ¶
type Maybe ¶ added in v0.18.0
type Maybe[T any] struct { // contains filtered or unexported fields }
Maybe is a container that can hold a value of type T. Just(v) returns a Maybe holding the value v. Absent() returns a Maybe that holds no value. A zero Maybe is ready for use and is equivalent to Absent().
It is safe to copy and assign a Maybe value, but note that if a value is present, only a shallow copy of the underlying value is made. Maybe values are comparable if and only if T is comparable.
Example ¶
package main import ( "fmt" "github.com/creachadair/mds/value" ) var randomValues = []int{1, 6, 16, 19, 4} func main() { even := make([]value.Maybe[int], 5) for i, r := range randomValues { if r%2 == 0 { even[i] = value.Just(r) } } var count int for _, v := range even { if v.Present() { count++ } } fmt.Println("input:", randomValues) fmt.Println("result:", even) fmt.Println("count:", count) }
Output: input: [1 6 16 19 4] result: [Absent[int] 6 16 Absent[int] 4] count: 3
func Absent ¶ added in v0.18.0
Absent returns a Maybe holding no value. A zero Maybe is equivalent to Absent().
func (Maybe[T]) Get ¶ added in v0.18.0
func (m Maybe[T]) Get() T
Get returns value held in m, if present; otherwise it returns the zero of T.
func (Maybe[T]) GetOK ¶ added in v0.18.0
GetOK reports whether m holds a value, and if so returns that value. If m is empty, GetOK returns the zero of T.
func (Maybe[T]) Or ¶ added in v0.18.0
Or returns m if m holds a value; otherwise it returns Just(o).