Resultat

sealed class Resultat<out T>

A sealed class that encapsulates a successful outcome with a value of type T or a failure with an arbitrary Throwable exception, or a loading state

See also

: This is a fork of Kotlin kotlin.Result class with an additional Loading state, but preserving Result API

Types

Link copied to clipboard
object Companion

Companion object for Resultat class that contains its constructor functions success, loading and failure.

Link copied to clipboard
data class Failure(val exception: Throwable) : Resultat<Nothing>

This type represents a failed outcome.

Link copied to clipboard
class Loading : Resultat<Nothing>

This type represents a loading state.

Link copied to clipboard
data class Success<out T>(val value: T) : Resultat<T>

This type represent a successful outcome.

Functions

Link copied to clipboard
fun exceptionOrNull(): Throwable?

Returns the encapsulated Throwable exception if this instance represents failure or null if it is success or loading.

Link copied to clipboard
inline fun getOrNull(): T?

Returns the encapsulated value if this instance represents success or null if it is failure or Resultat.Loading.

Properties

Link copied to clipboard
val isFailure: Boolean

Returns true if this instance represents a failed outcome. In this case isSuccess returns false. In this case isLoading returns false.

Link copied to clipboard
val isLoading: Boolean

Returns true if this instance represents a loading outcome. In this case isSuccess returns false. In this case isFailure returns false.

Link copied to clipboard
val isSuccess: Boolean

Returns true if this instance represents a successful outcome. In this case isFailure returns false. In this case isLoading returns false.

Inheritors

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
inline fun <R, T> Resultat<T>.fold(    onSuccess: (value: T) -> R,     onFailure: (exception: Throwable) -> R,     onLoading: () -> R): R

Returns the result of onSuccess for the encapsulated value if this instance represents success or the result of onFailure function for the encapsulated Throwable exception if it is failure. or the result of onLoading function if it is loading.

Link copied to clipboard
inline fun <R, T : R> Resultat<T>.getOrDefault(defaultValue: R): R

Returns the encapsulated value if this instance represents success or the defaultValue if it is failure or loading.

Link copied to clipboard
fun <R, T : R> Resultat<T>.getOrElse(onFailure: (exception: Throwable) -> R): R

Returns the encapsulated value if this instance represents success or the result of onFailure function for the encapsulated Throwable exception if it is failure or is loading.

Link copied to clipboard
fun <T> Resultat<T>.getOrThrow(): T

Returns the encapsulated value if this instance represents success or throws the encapsulated Throwable exception if it is failure.

Link copied to clipboard
inline fun <R, T> Resultat<T>.map(transform: (value: T) -> R): Resultat<R>

Returns the encapsulated result of the given transform function applied to the encapsulated value if this instance represents success or the original encapsulated Throwable exception if it is failure or loading.

Link copied to clipboard
inline fun <R, T> Resultat<T>.mapCatching(transform: (value: T) -> R): Resultat<R>

Returns the encapsulated result of the given transform function applied to the encapsulated value if this instance represents success or the original encapsulated Throwable exception if it is failure or the loading state if it loading.

Link copied to clipboard
inline fun <T> Resultat<T>.onFailure(action: (exception: Throwable) -> Unit): Resultat<T>

Performs the given action on the encapsulated Throwable exception if this instance represents failure. Returns the original Resultat unchanged.

Link copied to clipboard
inline fun <T> Resultat<T>.onLoading(action: () -> Unit): Resultat<T>

Performs the given action on the encapsulated value if this instance represents success. Returns the original Resultat unchanged.

Link copied to clipboard
inline fun <T> Resultat<T>.onSuccess(action: (value: T) -> Unit): Resultat<T>

Performs the given action on the encapsulated value if this instance represents success. Returns the original Resultat unchanged.

Link copied to clipboard
inline fun <R, T : R> Resultat<T>.recover(recoverLoading: Boolean = false, transform: (exception: Throwable) -> R): Resultat<R>

Returns the encapsulated result of the given transform function applied to the encapsulated Throwable exception if this instance represents failure or the original encapsulated value if it is success.

Link copied to clipboard
inline fun <R, T : R> Resultat<T>.recoverCatching(recoverLoading: Boolean = false, transform: (exception: Throwable) -> R): Resultat<R>

Returns the encapsulated result of the given transform function applied to the encapsulated Throwable exception if this instance represents failure or the original encapsulated value if it is success.

Link copied to clipboard
fun <T> Resultat<T>.toResult(): Result<T>?

Convert Resultat to Kotlin Result if Resultat is Resultat.Loading, null is returned