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

Make FlowCollector fun interface, remove redundant extensions #3047

Merged
merged 1 commit into from
Nov 25, 2021

Conversation

qwwdfsad
Copy link
Contributor

  • It also saves us from copy-without-imports problem

Addresses #3037

* It also saves us from copy-without-imports problem

Addresses #3037
Copy link
Collaborator

@dkhalanskyjb dkhalanskyjb left a comment

Choose a reason for hiding this comment

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

Found several implementations of FlowCollector.

  • Collect.kt. That implementation can be replaced with
public suspend inline fun <T> Flow<T>.collectIndexed(crossinline action: suspend (index: Int, value: T) -> Unit) {
    var index = 0
    collect { value ->
        action(checkIndexOverflow(index++), value)
    }
}
  • Limit.kt. Doesn't simplify easily.
  • In benchmarks, FlowPlaysScrabbleOpt. Simplifies easily.

These simplifications are not obviously better, so it's up to you whether to make them.

@qwwdfsad
Copy link
Contributor Author

I've deliberately avoided touching benchmarks.
Regarding collectIndexed -- the proposed implementation is not identical, capturing mutable ints will be boxed on each emission, while spilling it into the state of flow collector explicitly leaves it as int type

@ghost
Copy link

ghost commented Dec 21, 2021

Looks like after this change, coroutines 1.6.0 isn't backward compatible, when Flow<T>.collect(action) used in library. Is it expected?
originally reported at rsocket/rsocket-kotlin#201

@zjuhasz
Copy link

zjuhasz commented Jan 7, 2022

This change makes the documentation confusing in some places.

In SharedFlow.collect it says "To emit values from a shared flow into a specific collector ... collect { ... } extension should be used." but collect extension no longer exists.

In Flow.collect it says "To ensure the context preservation property, it is not recommended implementing this method directly. Instead, AbstractFlow can be used as the base type to properly ensure flow's properties." but every time a call to collect is made on any Flow we're anonymously implementing collect directly, usually with the SAM conversion.

@qwwdfsad
Copy link
Contributor Author

@zjuhasz thanks for noticing, shared flow documentation is fixed in #3127

but every time a call to collect is made on any Flow we're anonymously implementing collect directly, usually with the SAM conversion.

Flow interface is not anonymously implemented during collect SAM-conversion, only FlowCollector is, which is perfectly safe

yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this pull request Jan 28, 2022
…#3047)

* It also saves us from the copy-without-imports problem

Addresses Kotlin#3037
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this pull request Sep 14, 2022
…#3047)

* It also saves us from the copy-without-imports problem

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

Successfully merging this pull request may close these issues.

3 participants