Skip to content

Undeprecate Channel.receiveOrNull #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

elizarov
Copy link
Contributor

  • It is a valid method for channels of non-null elements that is
    also consistent with Kotlin stdlib tradition.
  • However, leave Channel.onReceiveOrNull since this is a tricker one,
    because on failure it aborts the whole select without letting figure
    out which clause failed.

* It is a valid method for channels of non-null elements that is
  also consistent with Kotlin stdlib tradition.
* However, leave Channel.onReceiveOrNull since this is a tricker one,
  because on failure it aborts the whole select without letting figure
  out which clause failed.
@elizarov elizarov requested a review from qwwdfsad October 20, 2018 16:55
@jcornaz
Copy link
Contributor

jcornaz commented Oct 22, 2018

What about having this kind of function instead?

suspend inline fun <E> ReceiveChannel<E>.receiveOr(action: () -> E): E {
  return try {
    receive()
  } catch (closedException: ClosedReceiveChannelException) {
    action()
  }
}

This let the user choose what to do if the the channel is closed.

val nullableValue = channel.receiveOr { null }
val nonNullableValue1 = channel.receiveOr { return }
val nonNullableValue2 = channel.receiveOr { throw Exception() }

@elizarov
Copy link
Contributor Author

@qwwdfsad Please, review and merge for 1.0.0 release

@qwwdfsad
Copy link
Collaborator

Fixed in develop.
Deprecation is removed, but "obsolete" marker is not.

@qwwdfsad qwwdfsad closed this Oct 23, 2018
twyatt added a commit to JuulLabs/able that referenced this pull request Oct 26, 2018
Typical behavior of `Channel` is to throw a
`ClosedReceiveChannelException` on invocation of `close` if a `receive`
is suspended. This is unfortunately a bit counter intuitive and can
produce misleading stacktraces.

In order to be more explicit with our exception stacktraces related to
`Channel` cancelation, using `receiveOrNull` and explicitly throwing
`GattClosed` exception when receiving `null`.

May need to revisit this behavior in the future after `receiveOrClosed`
is implemented per Kotlin/kotlinx.coroutines#330. Also noteworthy is
that `receiveOrNull` was undeprecated via Kotlin/kotlinx.coroutines#739.
@qwwdfsad qwwdfsad deleted the receive-or-null branch April 23, 2019 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants