简体   繁体   English

调用 CoroutineScope.launch 时,Dispatchers.Default 是如何转换为 CoroutineContext 的?

[英]When calling CoroutineScope.launch, how is Dispatchers.Default converted to a CoroutineContext?

I'm trying to understand coroutine scopes and have the following code:我正在尝试了解协程范围并具有以下代码:

launch(Dispatchers.Default) { ... }

The signature for launch is: launch的签名是:

public fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job

And the type of Dispatchers.Default is: Dispatchers.Default的类型是:

public abstract class CoroutineDispatcher :
    AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor

Neither AbstractCoroutineContextElement or ContinuationInterceptor implement CoroutineContext so how is the compiler able to coerce Dispatchers.Default into a CoroutineContext when launch is called? AbstractCoroutineContextElementContinuationInterceptor都没有实现CoroutineContext那么编译器如何在调用launch时将Dispatchers.Default强制转换为CoroutineContext呢?

Dispatchers.Default is a CoroutineContext . Dispatchers.Default是一个CoroutineContext

AbstractCoroutineContextElement implements CoroutineContext.Element which implements CoroutineContext . AbstractCoroutineContextElement实现CoroutineContext.Element ,后者实现CoroutineContext

Put another way, a CoroutineContext has a set of Element s, and each one is a CoroutineContext in its own right.换句话说,一个CoroutineContext有一组Element ,每个元素本身就是一个CoroutineContext

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 CoroutineScope(Dispatchers.Default).launch{} 中访问 getter()? - How to access getter() in CoroutineScope(Dispatchers.Default).launch{}? 如何对调用 CoroutineScope.launch 的 function 进行单元测试 - how to unit test a function which called CoroutineScope.launch 哪些用例适合 Dispatchers.Default 在 Kotlin 中? - Which usecases are suitable for Dispatchers.Default in Kotlin? 类中的GlobalScope.launch(coroutineContext)vs launch()扩展了CoroutineScope - GlobalScope.launch(coroutineContext) vs launch() in a class extends CoroutineScope 为什么CoroutineScope.launch和Coroutine.async是扩展函数而不是CoroutineScope的成员函数? - Why is CoroutineScope.launch and Coroutine.async are extension functions instead of a member function of CoroutineScope? Kotlin 协程中的 Dispatchers.Main 和 Dispatchers.Default 有什么区别? - What is the difference between Dispatchers.Main and Dispatchers.Default in Kotlin coroutines? 在协程范围内调用时启动不执行 - Launch not executing when called inside a coroutinescope 为什么 CoroutineExceptionHandler 只能在 coroutineContext 为 MainScope() 时执行启动? - Why CoroutineExceptionHandler can only perform launch when the coroutineContext is MainScope()? 对于 Kotlin Coroutine,使用 CoroutineScope 启动时,parentJob 放在哪里? - For Kotlin Coroutine, where to put parentJob, when using CoroutineScope to launch? Kotlin CoroutineScope初始化取决于具有自定义getter的CoroutineContext - Kotlin CoroutineScope initialization depending upon CoroutineContext with custom getter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM