Documentation
¶
Index ¶
- func Match[T, U any](r Result[T], ok func(T) U, err func(error) U) U
- func Partition[T any](results []Result[T]) (ok []T, errs []error)
- type Result
- func AndThen[T, U any](r Result[T], fn func(T) Result[U]) Result[U]
- func Err[T any](err error) Result[T]
- func Fold[T, U any](results []Result[T], initial U, fn func(acc U, val T) U) Result[U]
- func FoldAll[T any](results []Result[T]) Result[[]T]
- func Map[T, U any](r Result[T], fn func(T) U) Result[U]
- func MockSuccess[T any](value T, message string) Result[T]
- func Ok[T any](value T) Result[T]
- func PartitionResults[T any](results []Result[T]) (ok, errs []Result[T])
- func Sequence[T any](results []Result[T]) Result[[]T]
- func Traverse[T, U any](items []T, fn func(T) Result[U]) Result[[]U]
- func (r Result[T]) Error() error
- func (r Result[T]) Filter(predicate func(T) bool, errMsg string) Result[T]
- func (r Result[T]) FilterWithError(predicate func(T) bool, err error) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) OrElse(fallback Result[T]) Result[T]
- func (r Result[T]) SafeError() (error, bool)
- func (r Result[T]) SafeValue() (T, error)
- func (r Result[T]) Tap(fn func(T)) Result[T]
- func (r Result[T]) TapErr(fn func(error)) Result[T]
- func (r Result[T]) Unless(fn func(error)) Result[T]
- func (r Result[T]) Unwrap() (T, error)
- func (r Result[T]) UnwrapOr(default_ T) T
- func (r Result[T]) Validate(predicate func(T) bool, errorMsg string) Result[T]
- func (r Result[T]) ValidateWithError(predicate func(T) bool, err error) Result[T]
- func (r Result[T]) Value() T
- func (r Result[T]) When(fn func(T)) Result[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result is a type-safe way to return values or errors.
func AndThen ¶
AndThen chains operations that return Result, flattening the result. If the result is an error, it returns an error result without calling the function. This enables chaining operations that can fail (like FlatMap in functional programming).
func Fold ¶
Fold reduces a slice of Results into a single Result by applying fn cumulatively. The accumulator starts with initial value. If any result is an error, Fold returns that error immediately (short-circuit).
func FoldAll ¶
FoldAll reduces a slice of Results into a single Result by collecting all Ok values. If any result is an error, FoldAll returns that error immediately (short-circuit). Returns a slice of all values.
func MockSuccess ¶
MockSuccess creates a successful result with warning message.
func PartitionResults ¶
PartitionResults separates results into successful and failed Results.
func Sequence ¶
Sequence converts a slice of Results to a Result of slice. If all results are Ok, returns Ok with all values. If any result is an error, returns Err with the first error encountered.
func Traverse ¶
Traverse applies fn to each item in items and sequences the results. If all results are Ok, returns Ok with all transformed values. If any result is an error, returns Err with the first error encountered.
func (Result[T]) Filter ¶
Filter returns the result if predicate is true, otherwise returns an error. If the result already has an error, it passes through the error.
func (Result[T]) FilterWithError ¶
FilterWithError returns the result if predicate is true, otherwise returns the provided error. If the result already has an error, it passes through the error.
func (Result[T]) OrElse ¶
OrElse returns the current result if successful, otherwise returns the fallback result. Useful for providing fallback values when operations fail.
func (Result[T]) Tap ¶
Tap applies a side-effect function to the value if successful. Returns the original result unchanged, useful for logging or other side effects.
func (Result[T]) TapErr ¶
TapErr applies a side-effect function to the error if present. Returns the original result unchanged, useful for error logging.
func (Result[T]) Unless ¶
Unless executes the given function if the result is an error, otherwise does nothing. Returns the original result for chaining.
func (Result[T]) UnwrapOr ¶
func (r Result[T]) UnwrapOr(default_ T) T
UnwrapOr returns value or default if error.
func (Result[T]) Validate ¶
Validate checks if the value satisfies the predicate. If the predicate returns false, it returns an error with the given message. If the result already has an error, it passes through the error.
func (Result[T]) ValidateWithError ¶
ValidateWithError checks if the value satisfies the predicate. If the predicate returns false, it returns the provided error. If the result already has an error, it passes through the error.