Whence comes the context for new coroutines?

When you call block.startCoroutine(completion) or block.createCoroutineUnintercepted(completion), is the context for the new coroutine (and the Continuation returned by createCoroutineUnintercepted) copied from completion.context?

That should really be in the docs, but it isn’t.

Poking around the source code shows that the answer is yes, as expected (where else would it come from?).

Why are you investigating these methods? The expected way to create a coroutine is by calling launch or async.

Thanks for looking that up. Where did you find it? There isn’t any other obvious place where it would come from, but I didn’t think that getting it from the completion was really obvious either. At the time I didn’t otherwise care what context was used to resume the completion, so I used EmptyCoroutineContext

I’m using these methods in the implementation of this super-fun function, which is part of an Input/OutputStream replacement library:

/**
 * Create a coroutine that is executed incrementally as the returned
 * iterator is advanced.
 *
 * Until the end of the iterated sequence, the coroutine only executes
 * during calls to [Iterator.next].
 *
 * Each call to [Iterator.next] returns a suspending function that
 * should be called before [Iterator.next] is called again.  These functions
 * are produced out to the caller whenever the coroutine suspends, and
 * calling the produced function will delay until the coroutine is ready to
 * proceed.
 *
 * Suspending functions can also be produced out to the caller via
 * [IncrementalScope.yield] on the block's receiver.
 *
 * If the caller fails to call a produced suspending function when required,
 * or fails to interact with other side-effects of the coroutine in any
 * required way, then the coroutine should terminate as soon as it regains
 * control.
 *
 * When the coroutine terminates, or when it is cancelled via cancellation
 * of the [Job] in the parent [CoroutineScope], any remaining (clean up) work
 * will be launched in the parent scope, and subsequent calls to [Iterator.hasNext]
 * will return false.
 */
fun CoroutineScope.launchIncremental(block: suspend IncrementalScope.() -> Unit)
    :Iterator<suspend () -> Unit> {
    TODO()
}

That iterator of suspend lambdas looks kinda unwieldy. Have you looked at Flow?

I recommend sticking to the higher level methods described in the Coroutines Guide.

It’s got a different purpose. It’s more like a way to remove the scope restrictions from Sequences