Skip to content

Commit 41b88af

Browse files
authored
Update Flow.collect documentation (#3085)
1 parent 48aa354 commit 41b88af

File tree

1 file changed

+13
-5
lines changed
  • kotlinx-coroutines-core/common/src/flow

1 file changed

+13
-5
lines changed

kotlinx-coroutines-core/common/src/flow/Flow.kt

+13-5
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,25 @@ import kotlin.coroutines.*
172172
*
173173
* **The `Flow` interface is not stable for inheritance in 3rd party libraries**, as new methods
174174
* might be added to this interface in the future, but is stable for use.
175+
*
175176
* Use the `flow { ... }` builder function to create an implementation, or extend [AbstractFlow].
177+
* These implementations ensure that the context preservation property is not violated, and prevent most
178+
* of the developer mistakes related to concurrency, inconsistent flow dispatchers, and cancellation.
176179
*/
177180
public interface Flow<out T> {
181+
178182
/**
179183
* Accepts the given [collector] and [emits][FlowCollector.emit] values into it.
180-
* This method should never be implemented or used directly.
181184
*
182-
* The only way to implement the `Flow` interface directly is to extend [AbstractFlow].
183-
* To collect it into a specific collector, either `collector.emitAll(flow)` or `collect { ... }` extension
184-
* should be used. Such limitation ensures that the context preservation property is not violated and prevents most
185-
* of the developer mistakes related to concurrency, inconsistent flow dispatchers and cancellation.
185+
* This method can be used along with SAM-conversion of [FlowCollector]:
186+
* ```
187+
* myFlow.collect { value -> println("Collected $value")
188+
* ```
189+
*
190+
* ### Method inheritance
191+
*
192+
* To ensure the context preservation property, it is not recommended implementing this method directly.
193+
* Instead, [AbstractFlow] can be used as the base type to properly ensure flow's properties.
186194
*/
187195
public suspend fun collect(collector: FlowCollector<T>)
188196
}

0 commit comments

Comments
 (0)