ChannelResult

value class ChannelResult<out T>

A discriminated union of channel operation result. It encapsulates the successful or failed result of a channel operation or a failed operation to a closed channel with an optional cause.

The successful result represents a successful operation with a value of type T, for example, the result of Channel.receiveCatching operation or a successfully sent element as a result of Channel.trySend.

The failed result represents a failed operation attempt to a channel, but it doesn't necessary indicate that the channel is failed. E.g. when the channel is full, Channel.trySend returns failed result, but the channel itself is not in the failed state.

The closed result represents an operation attempt to a closed channel and also implies that the operation has failed. It is guaranteed that if the result is closed, then the target channel is either closed for send or is closed for receive depending on whether the failed operation was sending or receiving.

Types

Companion
Link copied to clipboard
common
object Companion

Functions

exceptionOrNull
Link copied to clipboard
common
fun exceptionOrNull(): Throwable?
Returns the encapsulated exception if this instance represents failure or null if it is success or unsuccessful operation to closed channel.
getOrNull
Link copied to clipboard
common
fun getOrNull(): T?
Returns the encapsulated value if this instance represents success or null if it represents failed result.
getOrThrow
Link copied to clipboard
common
fun getOrThrow(): T
Returns the encapsulated value if this instance represents success or throws an exception if it is closed or failed.
toString
Link copied to clipboard
common
open override fun toString(): String

Properties

isClosed
Link copied to clipboard
common
val isClosed: Boolean
Returns true if this instance represents unsuccessful operation to a closed or cancelled channel.
isFailure
Link copied to clipboard
common
val isFailure: Boolean
Returns true if this instance represents unsuccessful operation.
isSuccess
Link copied to clipboard
common
val isSuccess: Boolean
Returns true if this instance represents a successful operation outcome.

Extensions

getOrElse
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.getOrElse(onFailure: (exception: Throwable?) -> T): T
Returns the encapsulated value if this instance represents success or the result of onFailure function for the encapsulated Throwable exception if it is failed or closed result.
onClosed
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.onClosed(action: (exception: Throwable?) -> Unit): ChannelResult<T>
Performs the given action on the encapsulated Throwable exception if this instance represents failure due to channel being closed.
onFailure
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.onFailure(action: (exception: Throwable?) -> Unit): ChannelResult<T>
Performs the given action on the encapsulated Throwable exception if this instance represents failure.
onSuccess
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.onSuccess(action: (T) -> Unit): ChannelResult<T>
Performs the given action on the encapsulated value if this instance represents success.

Sources

common source
Link copied to clipboard