Package-level declarations

The core package hosts the main entrypoint of the library: The KResult class. It provides builders, transformers and extractors for result values and failures.

Types

Link copied to clipboard
sealed class KResult<out E, out T>

KResult and its inheritors Success and Failure provide an opinionated, functional result type. While Kotlin has its own kotlin.Result already, the intent of KResult is to be more functional, usable and better integrated.

Link copied to clipboard
typealias MultiErrorKResult<E, T> = KResult<List<E>, T>

Typealias to represent KResult types that have a List type on the Failure side

Link copied to clipboard
typealias MultiValueKResult<E, T> = KResult<E, List<T>>

Typealias to represent KResult types that have a List type on the Success side

Functions

Link copied to clipboard

Transforms any element to a KResult, carrying the given element on Failure side

Link copied to clipboard

Transforms any element to a MultiErrorKResult, carrying a list of elements on Failure side

Link copied to clipboard

Transforms a Kotlin native kotlin.Result to a KResult

Link copied to clipboard

Transforms a KResult to a Kotlin native kotlin.Result

Link copied to clipboard

Transforms any element to a KResult, carrying the given element on Success side

Link copied to clipboard

Transforms any element to a MultiValueKResult, carrying a list of elements on Success side

Link copied to clipboard
fun <E, T> KResult<E, T>.combine(other: KResult<E, T>, combineFailure: (E, E) -> E, combineSuccess: (T, T) -> T): KResult<E, T>
Link copied to clipboard
operator fun <E : Comparable<E>, T : Comparable<T>> KResult<E, T>.compareTo(other: KResult<E, T>): Int
Link copied to clipboard

Returns a list of errors

Link copied to clipboard
infix inline fun <E, T> KResult<E, T>.failureOrDefault(default: (T) -> E): E

Returns the error of the Failure side, or the result of the default function otherwise

Link copied to clipboard
inline fun <E, T> KResult<E, T>.filter(f: (success: T) -> Boolean, failureFn: (success: T) -> E): KResult<E, T>
Link copied to clipboard
inline fun <E, T, T1> KResult<E, T>.flatMap(f: (success: T) -> KResult<E, T1>): KResult<E, T1>
Link copied to clipboard
fun <E, T, E1> KResult<E, T>.flatMapFailure(f: (failure: E) -> KResult<E1, T>): KResult<E1, T>
Link copied to clipboard
fun <E, T> KResult<E, KResult<E, T>>.flatten(): KResult<E, T>
Link copied to clipboard
Link copied to clipboard
infix inline fun <E, T> KResult<E, T>.getOrDefault(default: (E) -> T): T

Returns the value of the Success side, or the result of the default function otherwise

Link copied to clipboard
infix inline fun <E, T> KResult<E, T>.getOrElse(default: (E) -> T): T
Link copied to clipboard
fun <E : Throwable, T> KResult<E, T>.getOrThrow(): T

If a KResult has a Throwable on failure side, this either returns the Success.value or throws the Failure.error

Link copied to clipboard
fun <T> KResult<T, T>.merge(): T
Link copied to clipboard
inline fun <E, T> MultiErrorKResult<E, T>.validate(failureValue: E, expectationFn: (success: T) -> Boolean): MultiErrorKResult<E, T>

Validates a Success or a FailureWithValue against a predicate (expectationFn] and applies failureValue if not fulfilled