opt

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package opt provides a generic Optional type that can be used to represent a value that may or may not be present.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

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

Optional is a generic type that encapsulates a value that may or may not be present. It provides methods to work with the value safely.

func Empty

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

Empty returns an empty Optional.

func Map

func Map[T, R any](o Optional[T], mapper func(T) R) Optional[R]

Map applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped value; otherwise, it returns an empty Optional.

func Of

func Of[T any](value T) Optional[T]

Of creates an Optional containing the provided value.

func OfNullable

func OfNullable[T any](value *T) Optional[T]

OfNullable creates an Optional from a pointer to a value. If the pointer is nil, it returns an empty Optional.

func (Optional[T]) Filter

func (o Optional[T]) Filter(filter func(T) bool) Optional[T]

Filter returns an Optional containing the value if it is present and satisfies the provided filter function; otherwise, it returns an empty Optional.

func (Optional[T]) Get

func (o Optional[T]) Get() T

Get retrieves the value contained in the Optional. It panics if the value is not present (i.e., if IsEmpty() returns true).

func (Optional[T]) IfPresent

func (o Optional[T]) IfPresent(consumer func(T))

IfPresent executes the provided consumer function with the value contained in the Optional if present.

func (Optional[T]) IfPresentOrElse

func (o Optional[T]) IfPresentOrElse(consumer func(T), or func())

IfPresentOrElse executes the provided consumer function with the value if present; otherwise, it executes the provided alternative function.

func (Optional[T]) IsEmpty

func (o Optional[T]) IsEmpty() bool

IsEmpty returns true if the Optional does not contain a value; otherwise, it returns false.

func (Optional[T]) IsPresent

func (o Optional[T]) IsPresent() bool

IsPresent returns true if the Optional contains a value; otherwise, it returns false.

func (Optional[T]) MapToAny

func (o Optional[T]) MapToAny(mapper func(T) any) Optional[any]

MapToAny applies the provided mapper function to the value contained in the Optional if present, returning a new Optional containing the mapped value; otherwise, it returns an empty Optional.

func (Optional[T]) Or

func (o Optional[T]) Or(supplier func() Optional[T]) Optional[T]

Or returns the value contained in the Optional if present; otherwise, it invokes the provided supplier function to obtain a new Optional.

func (Optional[T]) OrElse

func (o Optional[T]) OrElse(other T) T

OrElse returns the value contained in the Optional if present; otherwise, it returns the provided alternative value.

func (Optional[T]) OrElseGet

func (o Optional[T]) OrElseGet(supplier func() T) T

OrElseGet returns the value contained in the Optional if present; otherwise, it invokes the provided supplier function to obtain the value.

Jump to

Keyboard shortcuts

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