Kotlinx.coroutines: Flow.take does not accept 0

Created on 5 May 2019  路  2Comments  路  Source: Kotlin/kotlinx.coroutines

Most helpful comment

Thanks, I will fix the docs.

The problem with this fast-path is that one may expect that even for take(0) an original flow is started (=> start side-effects are observable), but with this change take(n) unexpectedly starts to behave differently in the terms of the source flow side-effects depending on the n value.
But other users may expect that flow is not started if n == 0 :)

I'd rather prohibit zero at all unless there is a serious demand on it.

All 2 comments

Makes sense to me. The code could use an early return here:

require(count >= 0) { "Requested element count $count should be positive" }
if(count==0) return emptyFlow()

Thanks, I will fix the docs.

The problem with this fast-path is that one may expect that even for take(0) an original flow is started (=> start side-effects are observable), but with this change take(n) unexpectedly starts to behave differently in the terms of the source flow side-effects depending on the n value.
But other users may expect that flow is not started if n == 0 :)

I'd rather prohibit zero at all unless there is a serious demand on it.

Was this page helpful?
0 / 5 - 0 ratings