Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion kotlinx-coroutines-core/common/src/Builders.common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,25 @@ public suspend fun <T> withContext(
* completes, and returns the result.
*
* This inline function calls [withContext].
*
* Example usage:
* ```
* Dispatchers.IO { someFile.exists() } // Checking the file system is blocking I/O.
* ```
*
* The code above is a shorthand to `withContext(Dispatchers.IO) { someFile.exists() }`,
* with the added safety that **only the dispatcher** can be replaced, i.e. it's not possible
* to do `randomCoroutineContext { someFile.exists() }` as it would be possible using
* `withContext`, so it's not possible to unintendedly break structured concurrency with this operator.
*/
public suspend inline operator fun <T> CoroutineDispatcher.invoke(
noinline block: suspend CoroutineScope.() -> T
): T = withContext(this, block)
): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withContext(this, block)
}

// --------------- implementation ---------------

Expand Down