core / pragma / PragmaResult /

PragmaResult #

sealed class PragmaResult<out TSuccess, out TFailure>

A discriminated union that encapsulates a successful outcome with a value of type TSuccess or a failure with of type TFailure. Similar to kotlin.Result, but with non-exception failures.

Constructors #

PragmaResult

protected fun PragmaResult()

Functions #

NameSummary

failureOrNull

fun failureOrNull(): TFailure?

Returns the held value if this is a failure, or null if not.

fold

inline fun <TFoldResult> fold(onSuccess: (TSuccess) -> TFoldResult, onFailure: (TFailure) -> TFoldResult): TFoldResult

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

getOrNull

fun getOrNull(): TSuccess?

Returns the held value if this is a success, or null if not.

getOrThrow

fun getOrThrow(): TSuccess

Returns the held value if this is a success, or throws an IllegalStateException if not.

onFailure

fun onFailure(action: (TFailure) -> Unit): PragmaResult<TSuccess, TFailure>

Performs the given action on the encapsulated value if this instance represents failure. Returns the original PragmaResult unchanged.

onSuccess

inline fun onSuccess(action: (TSuccess) -> Unit): PragmaResult<TSuccess, TFailure>

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

Extensions #

NameSummary

andThen

inline fun <TNewSuccess, TNewFailure, TSuccess, TFailure : TNewFailure> PragmaResult<TSuccess, TFailure>.andThen(onSuccess: (TSuccess) -> PragmaResult<TNewSuccess, TNewFailure>): PragmaResult<TNewSuccess, TNewFailure>

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

failureOrElse

inline fun <TNewFailure, TSuccess, TFailure : TNewFailure> PragmaResult<TSuccess, TFailure>.failureOrElse(onFailure: (TSuccess) -> TFailure): TFailure

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

getOrDefault

inline fun <TSuccess, TFailure> PragmaResult<TSuccess, TFailure>.getOrDefault(onFailure: () -> TSuccess): TSuccess

Returns the encapsulated value if this instance represents success or the result of if it is failure.

getOrElse

inline fun <TNewSuccess, TSuccess : TNewSuccess, TFailure> PragmaResult<TSuccess, TFailure>.getOrElse(onFailure: (TFailure) -> TSuccess): TSuccess

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

map

inline fun <TNewSuccess, TSuccess : TNewSuccess, TNewFailure, TFailure : TNewFailure> PragmaResult<TSuccess, TFailure>.map(transform: (TSuccess) -> TNewSuccess): PragmaResult<TNewSuccess, TNewFailure>

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

orElse

inline fun <TNewSuccess, TNewFailure, TSuccess : TNewSuccess, TFailure> PragmaResult<TSuccess, TFailure>.orElse(onFailure: (TFailure) -> PragmaResult<TNewSuccess, TNewFailure>): PragmaResult<TNewSuccess, TNewFailure>

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

recover

inline fun <TNewSuccess, TSuccess : TNewSuccess, TFailure> PragmaResult<TSuccess, TFailure>.recover(transform: (TFailure) -> TNewSuccess): TNewSuccess

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