[英]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? AbstractCoroutineContextElement
或ContinuationInterceptor
都没有实现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.