optional

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package optional provides a type which can be used to represent a value that may or may not be present like option in Rust.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type O

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

func Empty

func Empty[T any]() O[T]

Empty creates an empty Optional with no value.

func FromValue

func FromValue[T any](v T) O[T]

FromValue creates an Optional from a value.

func FromValue2

func FromValue2[T any](v T, ok bool) O[T]

FromValue2 creates an Optional from a value and a boolean meaning whether the value is valid.

HINT:

if we have a function defined as fn () (T, bool),
we can use FromValue2(fn()) instead of
if v, ok := fn(); ok { FromValue(v) } else { Empty[T]() }

func (O[T]) Must

func (o O[T]) Must() T

Must directly return the value of the Optional.

❌WARNING: Panic if the Optional has no value.

func (O[T]) Ok

func (o O[T]) Ok() bool

Ok returns whether the Optional has a valid value.

func (O[T]) Ptr

func (o O[T]) Ptr() *T

Ptr returns a pointer to the value of the Optional. return nil if the Optional has no value.

func (O[T]) ValueOr

func (o O[T]) ValueOr(dft T) T

ValueOr returns the value of the Optional if it has a valid value, otherwise returns the given default value.

func (O[T]) ValueOrZero

func (o O[T]) ValueOrZero() T

ValueOrZero returns the value of the Optional if it has a valid value, otherwise returns the zero value of T.

Jump to

Keyboard shortcuts

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