optional

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2022 License: MIT Imports: 0 Imported by: 1

README

go-optional

An optional type for golang with generics

var v optional.Type[int] = optional.New(123)
if unwrapped, ok := v.Value(); ok {
    unwrapped // 123
}

v.IsNil()      // false
v.ForceValue() // 123

v2 := optional.Nil[int]()
v2.IsNil() // true

optional.Map(v, func(t int) string {
    return fmt.Sprintf("hello %d", t)
}) // Type[string]

v3 := optional.New(optional.New(123))
optional.Compact(v3) // Type[int]

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Type

type Type[T any] struct {
	// contains filtered or unexported fields
}

func Compact

func Compact[T any](wrapped Type[Type[T]]) Type[T]

func FromPtr

func FromPtr[T any](wrapped *T) Type[T]

func Map

func Map[T, U any](v Type[T], f func(T) U) Type[U]

func New

func New[T any](wrapped T) Type[T]

func Nil

func Nil[T any]() Type[T]

func (Type[T]) ForceValue

func (s Type[T]) ForceValue() T

ForceValue returns the wrapped content if not nil, or it will panic

func (Type[T]) IsNil

func (s Type[T]) IsNil() bool

func (Type[T]) Value

func (s Type[T]) Value() (v T, ok bool)

Value is a golang style unwrapping method

func (Type[T]) ValueOrDefault

func (s Type[T]) ValueOrDefault(d T) T

ValueOrDefault return d if IsNil

func (Type[T]) ValueOrLazyDefault

func (s Type[T]) ValueOrLazyDefault(f func() T) T

ValueOrLazyDefault return f() if IsNil

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL