result

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error = errors.Error

type Result

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

result_ of an operation.

Designed to replace the return pattern of (value, error). result_ is either a value or an error. Ideally, the internal err would be a non-interface type, but kept as error for backwards compatibility. The internal err is designed to be a singular error and not a linked list of errors. This removes a ton of complexity and uncertainty in the error chain and error usage/lifecycle.

Example
exampleFn := func(success bool) result.Result[int] {
	if success {
		return result.Ok(1)
	}

	return result.Err[int](errErrorFailure)
}

// Success result.
fmt.Println(exampleFn(true).IsOk())
fmt.Println(exampleFn(true).Error().String())
fmt.Println(exampleFn(true).Value())
fmt.Println(exampleFn(true).ValueOr(2))
fmt.Println(exampleFn(true).ValueOrPanic())

// Error result.
fmt.Println(exampleFn(false).IsOk())
fmt.Println(exampleFn(false).Error().String())
fmt.Println(exampleFn(false).Value())
fmt.Println(exampleFn(false).ValueOr(2))
//fmt.Println(exampleFn(false).ValueOrPanic()) // Will panic
Output:

true

1
1
1
false
failure
0
2

func Err

func Err[T any](err Error) Result[T]

Err result. Used upon failure.

func Ok

func Ok[T any](result T) Result[T]

Ok result. Used upon success.

func Then

func Then[T any, S any](self Result[T], f func(T) Result[S]) Result[S]

func (Result[T]) Error

func (self Result[T]) Error() Error

Error of the result.

func (Result[T]) IsOk

func (self Result[T]) IsOk() bool

IsOk then return true, false otherwise.

func (Result[T]) Result

func (self Result[T]) Result() (T, Error)

Result decomposes into the basic (T, error) return value.

Useful when decomposing into variables for custom evaluation.

func (Result[T]) Value

func (self Result[T]) Value() T

Value of the Ok result.

Note: If called on an error result, this will be the zero value of T.

func (Result[T]) ValueOr

func (self Result[T]) ValueOr(defaultValue T) T

ValueOr default value if not an Ok result.

func (Result[T]) ValueOrPanic

func (self Result[T]) ValueOrPanic() T

ValueOrPanic if not an Ok result.

type When

type When[T any, S any] Result[T]

func (When[T, S]) Then

func (self When[T, S]) Then(fn func(T) Result[S]) Result[S]

Jump to

Keyboard shortcuts

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