Skip to content

Commit 7053405

Browse files
authored
Adjust deprecation message of 'poll' and 'receiveOrNull', explain the rationale and provide an alternative (#2704)
1 parent c3d711b commit 7053405

File tree

1 file changed

+19
-4
lines changed
  • kotlinx-coroutines-core/common/src/channels

1 file changed

+19
-4
lines changed

kotlinx-coroutines-core/common/src/channels/Channel.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,18 @@ public interface ReceiveChannel<out E> {
302302
* * Its name was not aligned with the rest of the API and tried to mimic Java's queue instead.
303303
*
304304
* See https://github.com/Kotlin/kotlinx.coroutines/issues/974 for more context.
305+
*
306+
* ### Replacement note
307+
*
308+
* The replacement `tryReceive().getOrNull()` is a default that ignores all close exceptions and
309+
* proceeds with `null`, while `poll` throws an exception if the channel was closed with an exception.
310+
* Replacement with the very same 'poll' semantics is `tryReceive().onClosed { if (it != null) throw it }.getOrNull()`
305311
*/
306-
@Deprecated(level = DeprecationLevel.WARNING,
307-
message = "Deprecated in the favour of 'tryReceive'",
312+
@Deprecated(
313+
level = DeprecationLevel.WARNING,
314+
message = "Deprecated in the favour of 'tryReceive'. " +
315+
"Please note that the provided replacement does not rethrow channel's close cause as 'poll' did, " +
316+
"for the precise replacement please refer to the 'poll' documentation",
308317
replaceWith = ReplaceWith("tryReceive().getOrNull()")
309318
) // Warning since 1.5.0
310319
public fun poll(): E? {
@@ -322,12 +331,18 @@ public interface ReceiveChannel<out E> {
322331
* - Was throwing if the channel has failed even though its signature may suggest it returns 'null'
323332
* - It didn't really belong to core channel API and can be exposed as an extension instead.
324333
*
325-
* @suppress doc
334+
* ### Replacement note
335+
*
336+
* The replacement `receiveCatching().getOrNull()` is a safe default that ignores all close exceptions and
337+
* proceeds with `null`, while `receiveOrNull` throws an exception if the channel was closed with an exception.
338+
* Replacement with the very same `receiveOrNull` semantics is `receiveCatching().onClosed { if (it != null) throw it }.getOrNull()`.
326339
*/
327340
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
328341
@LowPriorityInOverloadResolution
329342
@Deprecated(
330-
message = "Deprecated in favor of receiveCatching",
343+
message = "Deprecated in favor of 'receiveCatching'. " +
344+
"Please note that the provided replacement does not rethrow channel's close cause as 'receiveOrNull' did, " +
345+
"for the detailed replacement please refer to the 'receiveOrNull' documentation",
331346
level = DeprecationLevel.ERROR,
332347
replaceWith = ReplaceWith("receiveCatching().getOrNull()")
333348
) // Warning since 1.3.0, error in 1.5.0, will be hidden in 1.6.0

0 commit comments

Comments
 (0)