Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow.onEmpty/ifEmpty operators #1890

Closed
elizarov opened this issue Mar 31, 2020 · 1 comment
Closed

Flow.onEmpty/ifEmpty operators #1890

elizarov opened this issue Mar 31, 2020 · 1 comment

Comments

@elizarov
Copy link
Contributor

There is request to add Flow.onEmpty and/or Flow.ifEmpty operators that would enable concise check for check for empty flows, example:

flow.onEmpty { error("Must have an element") } // error on empty flow
flow.onEmpty { emit(default) } // default value on empty flow
flow.ifEmpty { default } // same as above

Original request: https://kotlinlang.slack.com/archives/C1CFAFJSK/p1585630443171500

@ScottPierce
Copy link
Contributor

I have a multiplatform library that's being shared internally at my company. We've exposed an API purposely that it can't emit any errors. At a lower level in the library we create a Flow, and emit the cached value (if there is one), and the server value (if it succeeds). This lower level of the library doesn't have access to the default value that is provided by the user. It's far easier to provide a default value at the higher level, which is what this operator would be used for. It's important that we always output at least one value.

qwwdfsad added a commit that referenced this issue Apr 6, 2020
qwwdfsad added a commit that referenced this issue Apr 24, 2020
* Introduce Flow.onEmpty operator

Fixes #1890
recheej pushed a commit to recheej/kotlinx.coroutines that referenced this issue Dec 28, 2020
* Introduce Flow.onEmpty operator

Fixes Kotlin#1890
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants