Documentation
¶
Overview ¶
Package either provides a sum type representing a value of one of two types.
Convention: Left represents failure/error, Right represents success. Mnemonic: "Right is right" (correct).
Either is right-biased: Map, MustGet, IfRight, GetOr operate on the Right value. Use MapLeft, MustGetLeft, IfLeft, LeftOr for Left-side operations.
Index ¶
- func Fold[L, R, T any](e Either[L, R], onLeft func(L) T, onRight func(R) T) T
- type Either
- func (e Either[L, R]) Get() (_ R, _ bool)
- func (e Either[L, R]) GetLeft() (_ L, _ bool)
- func (e Either[L, R]) GetOr(defaultVal R) R
- func (e Either[L, R]) GetOrCall(fn func() R) R
- func (e Either[L, R]) IfLeft(fn func(L))
- func (e Either[L, R]) IfRight(fn func(R))
- func (e Either[L, R]) IsLeft() bool
- func (e Either[L, R]) IsRight() bool
- func (e Either[L, R]) LeftOr(defaultVal L) L
- func (e Either[L, R]) LeftOrCall(fn func() L) L
- func (e Either[L, R]) Map(fn func(R) R) Either[L, R]
- func (e Either[L, R]) MustGet() R
- func (e Either[L, R]) MustGetLeft() L
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Either ¶
type Either[L, R any] struct { // contains filtered or unexported fields }
Either represents a value of one of two types. Convention: Left for failure, Right for success.
func Map ¶ added in v0.8.1
Map applies fn to the Right value and returns a new Either with a different Right type. If e is Left, returns the Left value unchanged.
func MapLeft ¶ added in v0.8.1
MapLeft applies fn to the Left value and returns a new Either with a different Left type. If e is Right, returns the Right value unchanged.
func (Either[L, R]) GetOr ¶ added in v0.11.0
func (e Either[L, R]) GetOr(defaultVal R) R
GetOr returns the Right value, or defaultVal if Left.
func (Either[L, R]) GetOrCall ¶ added in v0.8.1
func (e Either[L, R]) GetOrCall(fn func() R) R
GetOrCall returns the Right value, or the result of calling fn if e is Left.
func (Either[L, R]) IfLeft ¶ added in v0.23.0
func (e Either[L, R]) IfLeft(fn func(L))
IfLeft applies fn to the Left value if e is Left. If e is Right, does nothing.
func (Either[L, R]) IfRight ¶ added in v0.23.0
func (e Either[L, R]) IfRight(fn func(R))
IfRight applies fn to the Right value if e is Right. If e is Left, does nothing.
func (Either[L, R]) LeftOr ¶ added in v0.11.0
func (e Either[L, R]) LeftOr(defaultVal L) L
LeftOr returns the Left value, or defaultVal if Right.
func (Either[L, R]) LeftOrCall ¶ added in v0.8.1
func (e Either[L, R]) LeftOrCall(fn func() L) L
LeftOrCall returns the Left value, or the result of calling fn if e is Right.
func (Either[L, R]) Map ¶
Map applies fn to the Right value and returns a new Either. If e is Left, returns e unchanged.
func (Either[L, R]) MustGet ¶ added in v0.8.1
func (e Either[L, R]) MustGet() R
MustGet returns the Right value or panics if e is Left.
func (Either[L, R]) MustGetLeft ¶ added in v0.8.1
func (e Either[L, R]) MustGetLeft() L
MustGetLeft returns the Left value or panics if e is Right.