opt

package
v0.0.0-...-815d096 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package opt provides a generic optional value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opt

type Opt[T any] struct {
	Valid bool
	Value T
}

Opt is an optional value, an empty value is unset.

func Maybe

func Maybe[T comparable](value T) Opt[T]

Maybe returns a new set value if not zero, otherwise an unset value.

func New

func New[T any](value T) Opt[T]

New returns a new set value.

func NewIf

func NewIf[T any](value T, pred bool) Opt[T]

NewIf returns a new set value if the predicate is true, otherwise an unset value.

func None

func None[T any]() Opt[T]

None returns a new unset value.

func (*Opt[T]) Clear

func (o *Opt[T]) Clear() (v T, ok bool)

Clear clears the value and returns the previous one if any.

func (Opt[T]) MustUnwrap

func (o Opt[T]) MustUnwrap() T

MustUnwrap returns the value or panics if not set.

func (Opt[T]) Or

func (o Opt[T]) Or(default_ T) T

Or returns the value if set, otherwise returns the default value.

func (*Opt[T]) Set

func (o *Opt[T]) Set(value T)

Set sets the value.

func (*Opt[T]) SetIf

func (o *Opt[T]) SetIf(value T, predicate bool)

SetIf sets the value if the predicate is true.

func (Opt[T]) Unwrap

func (o Opt[T]) Unwrap() (T, bool)

Unwrap returns the value and true if set.

Jump to

Keyboard shortcuts

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