Try

sealed class Try<out Success, out Failure>

A Result type which can be used as a return type.

Inheritors

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class Failure<out S, out F>(val value: F) : Try<S, F>
Link copied to clipboard
class Success<out S, out F>(val value: S) : Try<S, F>

Properties

Link copied to clipboard
abstract val isFailure: Boolean
Link copied to clipboard
abstract val isSuccess: Boolean

Functions

Link copied to clipboard
fun <S, F> Try<S, F>.checkSuccess(): S

Returns value in case of success and throws an IllegalStateException in case of failure.

Link copied to clipboard
inline suspend fun <R> Try<ByteArray, ReadError>.decodeOrElse(decode: (value: ByteArray) -> Try<R, DecodeError>, recover: (DecodeError.Decoding) -> R): Try<R, ReadError>
Link copied to clipboard
inline suspend fun <R> Try<ByteArray, ReadError>.decodeOrNull(decode: (value: ByteArray) -> Try<R, DecodeError>): R?
Link copied to clipboard
abstract fun failureOrNull(): Failure?

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

Link copied to clipboard
inline fun <R, S, F> Try<S, F>.flatMap(transform: (value: S) -> Try<R, F>): Try<R, F>

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

Link copied to clipboard
inline fun <R> fold(onSuccess: (value: Success) -> R, onFailure: (exception: Failure) -> 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 value if it is failure.

Link copied to clipboard
fun <R, S : R, F> Try<S, F>.getOrDefault(defaultValue: R): R

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

Link copied to clipboard
inline fun <R, S : R, F> Try<S, F>.getOrElse(onFailure: (exception: F) -> R): R

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

Link copied to clipboard
abstract fun getOrNull(): Success?

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

Link copied to clipboard
fun <S, F : Throwable> Try<S, F>.getOrThrow(): S

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> map(transform: (value: Success) -> R): Try<R, Failure>

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.

Link copied to clipboard
inline fun <F> mapFailure(transform: (value: Failure) -> F): Try<Success, F>

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

Link copied to clipboard
inline fun onFailure(action: (exception: Failure) -> Unit): Try<Success, Failure>

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

Link copied to clipboard
inline fun onSuccess(action: (value: Success) -> Unit): Try<Success, Failure>

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

Link copied to clipboard
inline fun <R, S : R, F, T> Try<S, F>.tryRecover(transform: (exception: F) -> Try<R, T>): Try<R, T>

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