Skip to content

Commit

Permalink
Explicitly endorse usage of currentCoroutineContext() over kotlin.cor…
Browse files Browse the repository at this point in the history
…outines.coroutineContext (#4270)
  • Loading branch information
qwwdfsad authored Nov 7, 2024
1 parent a9e8a1e commit 588df14
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions kotlinx-coroutines-core/common/src/CoroutineScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,23 @@ public fun CoroutineScope.ensureActive(): Unit = coroutineContext.ensureActive()

/**
* Returns the current [CoroutineContext] retrieved by using [kotlin.coroutines.coroutineContext].
* This function is an alias to avoid name clash with [CoroutineScope.coroutineContext] in a receiver position:
* This function is an alias to avoid name clash with [CoroutineScope.coroutineContext]:
*
* ```
* launch { // this: CoroutineScope
* val flow = flow<Unit> {
* coroutineContext // Resolves into the context of outer launch, which is incorrect, see KT-38033
* currentCoroutineContext() // Retrieves actual context where the flow is collected
* }
* // ANTIPATTERN! DO NOT WRITE SUCH A CODE
* suspend fun CoroutineScope.suspendFunWithScope() {
* // Name of the CoroutineScope.coroutineContext in 'this' position, same as `this.coroutineContext`
* println(coroutineContext[CoroutineName])
* // Name of the context that invoked this suspend function, same as `kotlin.coroutines.coroutineContext`
* println(currentCoroutineContext()[CoroutineName])
* }
*
* withContext(CoroutineName("Caller")) {
* // Will print 'CoroutineName("Receiver")' and 'CoroutineName("Caller")'
* CoroutineScope("Receiver").suspendFunWithScope()
* }
* ```
*
* This function should always be preferred over [kotlin.coroutines.coroutineContext] property even when there is no explicit clash.
*/
public suspend inline fun currentCoroutineContext(): CoroutineContext = coroutineContext

0 comments on commit 588df14

Please sign in to comment.