...

/

Interface and Finding Elements in Coroutine Context

Interface and Finding Elements in Coroutine Context

Learn about CoroutineContext, its interface, and how to find elements within it.

If we look at the coroutine builders’ definitions, we’ll see that their first parameter is of type CoroutineContext.

Press + to interact
public fun CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job {
...
}

The receiver and the last argument’s receiver are of type CoroutineScope. Let’s clear up the terminology: launch is an extension function on CoroutineContext, so CoroutineContext is its receiver type. The extension function’s receiver is the object we reference with this. This CoroutineScope seems to be an essential concept, so let’s check out its definition.

Press + to interact
public interface CoroutineScope {
public val coroutineContext: CoroutineContext
}

It seems to be just a wrapper around CoroutineContext. So, we might want to recall how Continuation is defined.

Press + to interact
public interface Continuation<in T> {
public val context: CoroutineContext
public fun resumeWith(result: Result<T>)
}

The Continuation wrapper contains CoroutineContext as well. The most ...