result

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2022 License: MIT Imports: 0 Imported by: 0

README

Result

result is a type for functional programming

How to use

create
okInt := result.Ok(10)
errInt := result.Err[int] (errors.New("anything"))
unwrap
okInt := result.Ok(10)
errInt := result.Err[int] (errors.New("anything"))

if okInt.Ok() {
    fmt.Println(result.Unwrap())
}

if !errInt.Ok() {
    log.Fatal(result.Err())
}

map

when succeed
a := result.Ok(100)

a.Map(func (t int) (int, error)) {
    return t * 2, nil
})

if a.Ok() {
    fmt.Println(a.Unwrap())
}
200
when failed
a := result.Ok(100)

a.Map(func (t int) (int, error)) {
    return -1, errors.New("some error")
})

if !a.Ok() {
    fmt.Println(a.Err())
}
some error

map or

second parameter is default value

a := result.Ok(100)

a.MapOr(func (t int) (int, error)) {
    return t * 2, nil
}, 99)

fmt.Println(a.Unwrap())

a.MapOr(func (t int) (int, error)) {
    return -1, errors.New("some error")
}, 99)

fmt.Println(a.Unwrap())
200
99

and then

a := result.Ok(100)

a = a.AndThen(func(t int) (int, error) {
    return t * 2, nil
}).AndThen(func (t int) (int, error) {
    return t * 3, nil
})

if a.Ok() {
    fmt.Println(a.Unwrap())
}
600

unwrap or

a := result.Err[int](errors.New("Hello, World!"))

b := a.UnwrapOr(999)

fmt.Println(b)
999

unwrap or panic

a := result.Err[int](errors.New("Hello, World!"))

a.UnwrapOrPanic(errors.New("some error"))
some error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToFunctor

func ToFunctor[T any, R any](fn func(T) (R, error)) func(*Result[T]) *Result[R]

Types

type Result

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

func Err

func Err[T any](err error) *Result[T]

func Failed

func Failed[T any](err error) *Result[T]

func Map

func Map[T any, R any](param *Result[T], fn func(T) *Result[R]) *Result[R]

func Ok

func Ok[T any](data T) *Result[T]

func Success

func Success[T any](data T) *Result[T]

func (*Result[T]) AndThen

func (r *Result[T]) AndThen(fn func(T) (T, error)) *Result[T]

func (*Result[T]) Err

func (r *Result[T]) Err() error

func (*Result[T]) Map

func (r *Result[T]) Map(fn func(T) (T, error))

func (*Result[T]) MapOr

func (r *Result[T]) MapOr(fn func(T) (T, error), defaultValue T)

func (*Result[T]) Ok

func (r *Result[T]) Ok() bool

func (*Result[T]) Replace

func (r *Result[T]) Replace(fn func(T) (T, error))

must be removed

func (*Result[T]) ReplaceOr

func (r *Result[T]) ReplaceOr(fn func(T) (T, error), defaultValue T)

must be removed

func (*Result[T]) Unwrap

func (r *Result[T]) Unwrap() T

func (*Result[T]) UnwrapOr

func (r *Result[T]) UnwrapOr(defaultValue T) T

func (*Result[T]) UnwrapOrPanic

func (r *Result[T]) UnwrapOrPanic(err error) T

Jump to

Keyboard shortcuts

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