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

don*_*ner 7 intellij-idea kotlin kotlin-coroutines

我正在学习Kotlin 协程。第一个代码示例是:

fun main() = runBlocking { ... }
Run Code Online (Sandbox Code Playgroud)

的签名runBlocking

public actual fun <T> runBlocking(context: CoroutineContext, block: suspend CoroutineScope.() -> T): T
Run Code Online (Sandbox Code Playgroud)

没有为 指定默认值contextcontext在我没有指定并且没有指定默认值的情况下如何提供?

Wil*_*eed 3

乔佛里是正确的,默认值是EmptyCoroutineContext您可以在编写时的最新提交中看到默认值已被删除。

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

实际函数不能有默认参数值,它们应该在预期函数中声明

声明expect fun位于 中Builders.concurrent.kt,定义为EmptyCoroutineContext