ChannelResult
A discriminated union representing a channel operation result. It encapsulates the knowledge of whether the operation succeeded, failed with an option to retry, or failed because the channel was closed.
If the operation was successful, T is the result of the operation: for example, for ReceiveChannel.receiveCatching and ReceiveChannel.tryReceive, it is the element received from the channel, and for Channel.trySend, it is Unit, as the channel does not receive anything in return for sending a channel. This value can be retrieved with getOrNull or getOrThrow.
If the operation failed, it does not necessarily mean that the channel itself is closed. For example, ReceiveChannel.receiveCatching and ReceiveChannel.tryReceive can fail because the channel is empty, and Channel.trySend can fail because the channel is full.
If the operation failed because the channel was closed for that operation, isClosed returns true
. The opposite is also true: if isClosed returns true
, then the channel is closed for that operation (ReceiveChannel.isClosedForReceive or SendChannel.isClosedForSend). In this case, retrying the operation is meaningless: once closed, the channel will remain closed. The exceptionOrNull function returns the reason the channel was closed, if any was given.
Manually obtaining a ChannelResult instance is not supported. See the documentation for ChannelResult-returning functions for usage examples.
Properties
Functions
Returns the exception with which the channel was closed, or null
if the channel was not closed or was closed without a cause.
Returns the encapsulated T if the operation succeeded, or throws the encapsulated exception if it failed.
Performs the given action if the operation failed because the channel was closed for that operation. The result of ChannelResult.exceptionOrNull is passed to the action parameter.
Performs the given action if the operation failed. The result of ChannelResult.exceptionOrNull is passed to the action parameter.