@@ -150,7 +150,7 @@ public interface SendChannel<in E> {
150
150
level = DeprecationLevel .WARNING ,
151
151
message = " Deprecated in the favour of 'trySend' method" ,
152
152
replaceWith = ReplaceWith (" trySend(element).isSuccess" )
153
- )
153
+ ) // Since 1.5.0
154
154
public fun offer (element : E ): Boolean {
155
155
val result = trySend(element)
156
156
if (result.isSuccess) return true
@@ -198,7 +198,7 @@ public interface ReceiveChannel<out E> {
198
198
* Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed.
199
199
*
200
200
* This function can be used in [select] invocations with the [onReceive] clause.
201
- * Use [poll ] to try receiving from this channel without waiting.
201
+ * Use [tryReceive ] to try receiving from this channel without waiting.
202
202
*/
203
203
public suspend fun receive (): E
204
204
@@ -210,51 +210,6 @@ public interface ReceiveChannel<out E> {
210
210
*/
211
211
public val onReceive: SelectClause1 <E >
212
212
213
- /* *
214
- * This function was deprecated since 1.3.0 and is no longer recommended to use
215
- * or to implement in subclasses.
216
- *
217
- * It had the following pitfalls:
218
- * - Didn't allow to distinguish 'null' as "closed channel" from "null as a value"
219
- * - Was throwing if the channel has failed even though its signature may suggest it returns 'null'
220
- * - It didn't really belong to core channel API and can be exposed as an extension instead.
221
- *
222
- * @suppress doc
223
- */
224
- @Suppress(" INVISIBLE_REFERENCE" , " INVISIBLE_MEMBER" )
225
- @LowPriorityInOverloadResolution
226
- @Deprecated(
227
- message = " Deprecated in favor of receiveCatching" ,
228
- level = DeprecationLevel .ERROR ,
229
- replaceWith = ReplaceWith (" receiveCatching().getOrNull()" )
230
- ) // Warning since 1.3.0, error in 1.5.0, will be hidden in 1.6.0
231
- public suspend fun receiveOrNull (): E ? = receiveCatching().getOrNull()
232
-
233
- /* *
234
- * This function was deprecated since 1.3.0 and is no longer recommended to use
235
- * or to implement in subclasses.
236
- * See [receiveOrNull] documentation.
237
- *
238
- * @suppress **Deprecated**: in favor of onReceiveCatching extension.
239
- */
240
- @Deprecated(
241
- message = " Deprecated in favor of onReceiveCatching extension" ,
242
- level = DeprecationLevel .ERROR ,
243
- replaceWith = ReplaceWith (" onReceiveCatching" )
244
- ) // Warning since 1.3.0, error in 1.5.0, will be hidden or removed in 1.6.0
245
- public val onReceiveOrNull: SelectClause1 <E ?>
246
- get() {
247
- return object : SelectClause1 <E ?> {
248
- @InternalCoroutinesApi
249
- override fun <R > registerSelectClause1 (select : SelectInstance <R >, block : suspend (E ? ) -> R ) {
250
- onReceiveCatching.registerSelectClause1(select) {
251
- it.exceptionOrNull()?.let { throw it }
252
- block(it.getOrNull())
253
- }
254
- }
255
- }
256
- }
257
-
258
213
/* *
259
214
* Retrieves and removes an element from this channel if it's not empty, or suspends the caller while this channel is empty.
260
215
* This method returns [ChannelResult] with the value of an element successfully retrieved from the channel
@@ -272,7 +227,7 @@ public interface ReceiveChannel<out E> {
272
227
* Use [yield] or [CoroutineScope.isActive] to periodically check for cancellation in tight loops if needed.
273
228
*
274
229
* This function can be used in [select] invocations with the [onReceiveCatching] clause.
275
- * Use [poll ] to try receiving from this channel without waiting.
230
+ * Use [tryReceive ] to try receiving from this channel without waiting.
276
231
*/
277
232
public suspend fun receiveCatching (): ChannelResult <E >
278
233
@@ -283,17 +238,6 @@ public interface ReceiveChannel<out E> {
283
238
*/
284
239
public val onReceiveCatching: SelectClause1 <ChannelResult <E >>
285
240
286
- /* *
287
- * Retrieves and removes an element from this channel if it's not empty or returns `null` if the channel is empty
288
- * or is [is closed for `receive`][isClosedForReceive] without a cause.
289
- * It throws the original [close][SendChannel.close] cause exception if the channel has _failed_.
290
- */
291
- public fun poll (): E ? {
292
- val result = tryReceive()
293
- if (result.isSuccess) return result.getOrThrow()
294
- throw recoverStackTrace(result.exceptionOrNull() ? : return null )
295
- }
296
-
297
241
/* *
298
242
* Retrieves and removes an element from this channel if it's not empty, returning a [successful][ChannelResult.success]
299
243
* result, returns [failed][ChannelResult.failed] result if the channel is empty, and [closed][ChannelResult.closed]
@@ -335,6 +279,76 @@ public interface ReceiveChannel<out E> {
335
279
*/
336
280
@Deprecated(level = DeprecationLevel .HIDDEN , message = " Since 1.2.0, binary compatibility with versions <= 1.1.x" )
337
281
public fun cancel (cause : Throwable ? = null): Boolean
282
+
283
+
284
+ /* *
285
+ * **Deprecated** poll method.
286
+ *
287
+ * This method was deprecated in the favour of [tryReceive].
288
+ * It has proven itself as error-prone method in Channel API:
289
+ *
290
+ * * Nullable return type creates the false sense of security, implying that `null`
291
+ * is returned instead of throwing an exception.
292
+ * * It was used mostly from non-suspending APIs where CancellationException triggered
293
+ * internal failures in the application (the most common source of bugs).
294
+ * * Its name was not aligned with the rest of the API and tried to mimic Java's queue instead.
295
+ *
296
+ * See https://github.com/Kotlin/kotlinx.coroutines/issues/974 for more context.
297
+ */
298
+ @Deprecated(level = DeprecationLevel .WARNING ,
299
+ message = " Deprecated in the favour of 'tryReceive'" ,
300
+ replaceWith = ReplaceWith (" tryReceive().getOrNull()" )
301
+ ) // Since 1.5.0
302
+ public fun poll (): E ? {
303
+ val result = tryReceive()
304
+ if (result.isSuccess) return result.getOrThrow()
305
+ throw recoverStackTrace(result.exceptionOrNull() ? : return null )
306
+ }
307
+
308
+ /* *
309
+ * This function was deprecated since 1.3.0 and is no longer recommended to use
310
+ * or to implement in subclasses.
311
+ *
312
+ * It had the following pitfalls:
313
+ * - Didn't allow to distinguish 'null' as "closed channel" from "null as a value"
314
+ * - Was throwing if the channel has failed even though its signature may suggest it returns 'null'
315
+ * - It didn't really belong to core channel API and can be exposed as an extension instead.
316
+ *
317
+ * @suppress doc
318
+ */
319
+ @Suppress(" INVISIBLE_REFERENCE" , " INVISIBLE_MEMBER" )
320
+ @LowPriorityInOverloadResolution
321
+ @Deprecated(
322
+ message = " Deprecated in favor of receiveCatching" ,
323
+ level = DeprecationLevel .ERROR ,
324
+ replaceWith = ReplaceWith (" receiveCatching().getOrNull()" )
325
+ ) // Warning since 1.3.0, error in 1.5.0, will be hidden in 1.6.0
326
+ public suspend fun receiveOrNull (): E ? = receiveCatching().getOrNull()
327
+
328
+ /* *
329
+ * This function was deprecated since 1.3.0 and is no longer recommended to use
330
+ * or to implement in subclasses.
331
+ * See [receiveOrNull] documentation.
332
+ *
333
+ * @suppress **Deprecated**: in favor of onReceiveCatching extension.
334
+ */
335
+ @Deprecated(
336
+ message = " Deprecated in favor of onReceiveCatching extension" ,
337
+ level = DeprecationLevel .ERROR ,
338
+ replaceWith = ReplaceWith (" onReceiveCatching" )
339
+ ) // Warning since 1.3.0, error in 1.5.0, will be hidden or removed in 1.6.0
340
+ public val onReceiveOrNull: SelectClause1 <E ?>
341
+ get() {
342
+ return object : SelectClause1 <E ?> {
343
+ @InternalCoroutinesApi
344
+ override fun <R > registerSelectClause1 (select : SelectInstance <R >, block : suspend (E ? ) -> R ) {
345
+ onReceiveCatching.registerSelectClause1(select) {
346
+ it.exceptionOrNull()?.let { throw it }
347
+ block(it.getOrNull())
348
+ }
349
+ }
350
+ }
351
+ }
338
352
}
339
353
340
354
/* *
0 commit comments