Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does runBlocking supply a default value for context?

I am learning Kotlin coroutines. The first code example is:

fun main() = runBlocking { ... }

The signature for runBlocking is:

public actual fun <T> runBlocking(context: CoroutineContext, block: suspend CoroutineScope.() -> T): T

There is no default value specified for context. How is context supplied without me specifying it and with no default specified?

like image 560
donturner Avatar asked May 29 '26 18:05

donturner


1 Answers

Joffrey is correct that the default value is EmptyCoroutineContext. You can see in the latest commit as of writing the default value was removed.

You can see in that commit, the function was changed to actual fun, probably in support of some multiplatform changes per the commit message. If you have an expect fun with a default argument, and you also try adding it to the actual fun you get the error

Actual function cannot have default argument values, they should be declared in the expected function

The expect fun declaration is in Builders.concurrent.kt, defined with the EmptyCoroutineContext

like image 141
William Reed Avatar answered Jun 01 '26 20:06

William Reed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!