option

package
v0.0.0-...-2bc3700 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2023 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package option provides the Option type which represents an optional value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapOr

func MapOr[T any, E any](o Option[T], defaultVal E, f func(v T) E) E

MapOr returns the provided default result (if none), or applies a function to the contained value (if any).

Arguments passed to MapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use [Option.MapOrElse], which is lazily evaluated.

func MapOrElse

func MapOrElse[T any, E any](o Option[T], defaultFn func() E, f func(v T) E) E

MapOrElse returns the default function result (if none), or applies a function to the contained value (if any).

Types

type Option

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

Option represents an optional value.

Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not. Option types are very common in Rust code, as they have a number of uses.

Option in this package is a port of subset of Rust Option type.

One import usage of Options is an optional argument type of filters, tests, or functions.

func AndThen

func AndThen[T any, U any](o Option[T], f func(v T) U) Option[U]

AndThen returns None if the option is None, otherwise calls `f` with the wrapped value and returns the result.

Some languages call this operation flatmap.

func Map

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

Map maps an Option[T] to Option[U] by applying a function to a contained value (if Some) or returns None (if None).

func None

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

None creates a None value of type T.

func Some

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

Some creates a Some value of type T.

func (*Option[T]) AsPtr

func (o *Option[T]) AsPtr() *T

AsPtr returns the pointer to the contained data if the options is a Some value. It returns nil if the option is a None value.

func (Option[T]) Compare

func (o Option[T]) Compare(other Option[T], cmpData func(a, b T) int) int

Compare compares o and other and returns:

-1 if o <  other
 0 if o == other
+1 if o >  other

where None < Some and for case both options are Some, the contained data are compared with cmpData.

func (Option[T]) Equal

func (o Option[T]) Equal(other Option[T], eqData func(a, b T) bool) bool

Equal returns true if o and other are both Some values and and those values are equal or both None. It returns false otherwise.

func (Option[T]) Hash

func (o Option[T]) Hash(h hash.Hash, hashData func(data T, h hash.Hash))

Hash returns the hashed value of the option.

If the option is a Some value, the contained data are hashed with hashData.

func (Option[T]) IsNone

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

IsNone returns true if the option is a None value.

func (Option[T]) IsSome

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

IsSome returns true if the option is a Some value.

func (*Option[T]) Take

func (o *Option[T]) Take() Option[T]

Take takes the value out of the option, leaving a None in its place.

func (Option[T]) Unwrap

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

Unwrap returns the contained Some value if the options is a Some value. It panics if the the option is a None value.

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(defaultVal T) T

UnwrapOr returns the contained Some value or a provided default.

Arguments passed to UnwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use Option.UnwrapOrElse, which is lazily evaluated.

func (Option[T]) UnwrapOrElse

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

UnwrapOrElse returns the contained Some value or computes it from a closure.

func (Option[T]) UnwrapTo

func (o Option[T]) UnwrapTo(dest *T) bool

UnwrapTo sets the contained Some value to dest and returns true if the options is a Some value. It does nothing and returns false if the option is a None value.

This can be used with alternative of Rust's if let Some(dest) = o { ... } or while let Some(dest) = o { ... }

An example with if statement:

if v := (Value{}); o.UnwrapTo(&v) {
  // do something with v
}

An example with for statement and an Iterator.

for v := (Value{}); iter.Next().UnwrapTo(&v); {
  // do something with v
}

Jump to

Keyboard shortcuts

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