runBlocking 如何为上下文提供默认值?

How does runBlocking supply a default value for context?

我在学习Kotlin coroutines。第一个代码示例是:

fun main() = runBlocking { ... }

signature for runBlocking是:

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

没有为 context 指定默认值。 context 是如何在我未指定且未指定默认值的情况下提供的?

Joffrey 是正确的,默认值为 EmptyCoroutineContext。您可以在 the latest commit as of writing 中看到默认值已被删除。

您可以在该提交中看到,该功能已更改为 actual fun,可能是为了根据提交消息支持一些多平台更改。如果你有一个带有默认参数的 expect fun,并且你也尝试将它添加到 actual fun,你会得到错误

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

expect fun 声明在 Builders.concurrent.kt 中,用 EmptyCoroutineContext

定义