Skip to content

Fix the explanation of flow emit constraints #4391

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

Merged

Conversation

seyoungcho2
Copy link
Contributor

In this part, it says that it checks whether it's the same coroutine, but in the actual implementation, it checks whether it's the same CoroutineContext rather than the same coroutine.

Specifically, in the case of the withContext example, although it internally creates a DispatchedCoroutine object, this is only for context switching, and it still maintains the same coroutine.

val myFlow = flow {
     // GlobalScope.launch { // is prohibited
     // launch(Dispatchers.IO) { // is prohibited
     // withContext(CoroutineName("myFlow")) { // is prohibited
     emit(1) // OK
     coroutineScope {
         emit(2) // OK -- still the same coroutine
     }
 }

Therefore, it might be less confusing to describe this as checking for the same coroutine context rather than the same coroutine.

(cherry picked from commit 755347c)
@seyoungcho2 seyoungcho2 changed the title Fix an explanation of flow emit Fix the explanation of flow emit constraints Mar 23, 2025
@dkhalanskyjb dkhalanskyjb merged commit 8627cc3 into Kotlin:develop Mar 24, 2025
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.

2 participants