option

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: GPL-3.0 Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapOr

func MapOr[T any, U any](option Option[T], other U, f func(*T) U) U

Returns the provided default result (if none), or applies a function to the contained value (if any). This function is not a method of option because method must have no type parameter. https://github.com/golang/go/issues/48793

func MapOrElse

func MapOrElse[T any, U any](option Option[T], def func() U, f func(*T) U) U

Computes a default function result (if none), or applies a different function to the contained value (if any). This function is not a method of option because method must have no type parameter. https://github.com/golang/go/issues/48793

Types

type Option

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

This Option implementation is based on the one in the Rust's standart library (https://doc.rust-lang.org/std/option/enum.Option.html) The Option represents an optional value: every Option is either Some and contains a value, or None, and does not.

func Map

func Map[T any, U any](option Option[T], f func(*T) U) Option[U]

Maps an Option[T] to Option[U] by applying a function to a contained value. This function is not a method of option because method must have no type parameter. https://github.com/golang/go/issues/48793

func None

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

Return an Option containing no value.

func Some

func Some[T any](value T) Option[T]

Return an Option containing the value `value`.

func (Option[T]) Expect

func (option Option[T]) Expect(message string) T

Returns the contained Some value, consuming the option value. Panics if the value is a None with a custom panic message provided by message.

func (Option[T]) IsNone

func (option Option[T]) IsNone() bool

Returns true if the option is a None value.

func (Option[T]) IsSome

func (option Option[T]) IsSome() bool

Returns true if the option is a Some value.

func (Option[T]) IsSomeWith

func (option Option[T]) IsSomeWith(f func(*T) bool) bool

Returns true if the option is a Some wrapping a value matching the predicate.

func (Option[T]) Unwrap

func (option Option[T]) Unwrap() T

Returns the contained Some value, consuming the option value. Because this function may panic, its use is generally discouraged. Instead, prefer to use pattern matching and handle the None case explicitly, or call UnwrapOr, UnwrapOrElse, or UnwrapOrDefault. Panics if the self value equals None.

func (Option[T]) UnwrapOr

func (option Option[T]) UnwrapOr(other T) T

Returns the contained Some value or a provided default.

func (Option[T]) UnwrapOrDefault

func (option Option[T]) UnwrapOrDefault() T

Returns the contained Some value or a default. Consumes the self argument then, if Some, returns the contained value, otherwise if None, returns the default value for that type.

func (Option[T]) UnwrapOrElse

func (option Option[T]) UnwrapOrElse(f func() T) T

Returns the contained Some value or computes it from a closure.

Jump to

Keyboard shortcuts

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