Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion kotlinx-coroutines-core/common/src/channels/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,36 @@ public inline fun <T> ChannelResult<T>.onClosed(action: (exception: Throwable?)

/**
* Iterator for a [ReceiveChannel].
* Instances of this interface are *not thread-safe* and shall not be used from concurrent coroutines.
* Instances of this interface are *not thread-safe*.
* Each iterator instance should be used by a single coroutine.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not true, it can be passed around different coroutines as long as this doesn't result in multithreaded access.

*
* An example usage:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you describe the sequence of events where this sample would answer a question a user may realistically have? It neither explains the next()/hasNext() interplay useful in deeply technical cases where you'd manipulate ChannelIterator manually, nor does it spell out "hey, this is not the class that you need to use manually, just write for (... in ...) instead" if that was the intention.

*
* ```
* val channel = Channel<Int>()
* launch {
* channel.send(1)
* channel.send(2)
* channel.send(3)
* channel.close() // NB: must close for iterators to finish
* }
* launch {
* for (element in channel) {
* println("Consumer A got $element")
* }
* }
* launch {
* for (element in channel) {
* println("Consumer B got $element")
* }
* }
* ```
* Possible output:
* ```text
* Consumer A got 1
* Consumer A got 2
* Consumer B got 3
* ```
*/
public interface ChannelIterator<out E> {
/**
Expand Down