ChannelResult

value class ChannelResult<out T>(source)

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 necessarily 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.

Properties

Link copied to clipboard

Returns true if this instance represents unsuccessful operation to a closed or cancelled channel.

Link copied to clipboard

Returns true if this instance represents unsuccessful operation.

Link copied to clipboard

Returns true if this instance represents a successful operation outcome.

Functions

Link copied to clipboard

Returns the encapsulated exception if this instance represents failure or null if it is success or unsuccessful operation to closed channel.

Link copied to clipboard
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.

Link copied to clipboard
fun getOrNull(): T?

Returns the encapsulated value if this instance represents success or null if it represents failed result.

Link copied to clipboard
fun getOrThrow(): T

Returns the encapsulated value if this instance represents success or throws an exception if it is closed or failed.

Link copied to clipboard
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. The result of ChannelResult.exceptionOrNull is passed to the action parameter. It is guaranteed that if action is invoked, then the channel is either closed for send or is closed for receive depending on the failed operation.

Link copied to clipboard
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. The result of ChannelResult.exceptionOrNull is passed to the action parameter.

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

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

Link copied to clipboard
open override fun toString(): String