[swift-evolution] Contextualizing async coroutines

Brent Royal-Gordon brent at architechies.com
Sat Sep 2 03:57:20 CDT 2017

> On Aug 31, 2017, at 11:35 AM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> # Coroutine context
> # `onResume` hooks

`onResume` hooks seem like a really good way to, essentially, allow arbitrary concurrency primitives to be passed into `async` functions. My main question is, if we have it, why do we need coroutine contexts? It seems to me that anything the coroutine might do with the context could either be passed into its parameters, or encapsulated in its `onResume` hook.

> and also prevents writing APIs that intentionally change context across an `await`, like a theoretical "goToMainThread()" function

You say that like it's a bad thing. :^)

(Seriously, I feel like that makes it *way* too easy to jump to another thread in the middle of a function using what looks like an ordinary line of code. A `let result = await someQueue.async { … }` syntax is a lot clearer that you're hopping to another thread and about what code will run on it, but can get around our current "pyramid of doom" problem. If the async syntax guarantees that the code before and after the `await` runs in the same "environment"—whatever environment is set up by the onResume hook—then that makes async functions much safer and easier for programmers to reason about, while only restricting their power slightly.)

Brent Royal-Gordon

More information about the swift-evolution mailing list