Skip to content

Commit c20bc93

Browse files
committed
Prohibit Flow.take(0)
It can be treated ambiguously (do nothing vs cancel immediately), so we better wait for a user feedback with a use-cases before allowing this
1 parent 165fbaf commit c20bc93

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

kotlinx-coroutines-core/common/src/flow/operators/Limit.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public fun <T> Flow<T>.dropWhile(predicate: suspend (T) -> Boolean): Flow<T> = f
5050
*/
5151
@FlowPreview
5252
public fun <T> Flow<T>.take(count: Int): Flow<T> {
53-
require(count >= 0) { "Requested element count $count is less than zero." }
54-
if (count == 0) return emptyFlow()
53+
require(count > 0) { "Requested element count $count should be positive" }
5554
return flow {
5655
var consumed = 0
5756
try {

kotlinx-coroutines-core/common/test/flow/operators/TakeTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ class TakeTest : TestBase() {
2828
}
2929

3030
@Test
31-
fun testNonPositiveValues() = runTest {
31+
fun testNonPositiveValues() {
3232
val flow = flowOf(1)
3333
assertFailsWith<IllegalArgumentException> {
3434
flow.take(-1)
3535
}
3636

37-
assertNull(flow.take(0).singleOrNull())
37+
assertFailsWith<IllegalArgumentException> {
38+
flow.take(0)
39+
}
3840
}
3941

4042
@Test

0 commit comments

Comments
 (0)