First Draft: Coroutines

Sebastiaan Koppe mail at skoppe.eu
Mon Aug 5 15:50:08 UTC 2024


On Monday, 5 August 2024 at 01:36:31 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
> Coroutines is a stack machine transformed representation for a 
> function. It enables storing the state of a function externally 
> to the stack to allow for high throughput event handling.

Thank you for spearheading this. Having coroutines in the 
language would be a big step forward, and hopefully pave the way 
for more non-blocking programming in D.

There are a few points I would like to bring up early though.

- I see no mention of C++'s coroutines. I think it would be good 
to learn from their design and implementation.

- As you might know I am a big proponent of C++'s 
Senders/Receivers (a.k.a. P2300). One awesome integration they 
have is that Senders can be awaited by coroutines, and coroutines 
can await Senders. This allows for users to pick their 
preference, e.g. use convenient coroutines but suffer some 
allocation costs, and use Senders for more performant sections if 
need be. See
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2300r7.html#design-awaitables-are-senders and https://ericniebler.com/2024/02/04/what-are-senders-good-for-anyway/

- I don't understand the choice for `@async`. Sometimes 
resembling the word "coroutine" would be better in my opinion.

- In `while(Future!string readLine = socket.readUntil("\n"))` I 
suspect it to do an explicit await. How does that work?

- I find the use of `Future` a bit confusing. Futures generally 
carry too much synchronisation overhead with them and I doubt 
coroutines need a full Future implementation anyway. Perhaps call 
it a Task like they do in C++?

- The use of `@isasync` is a bit too cute for me. It also hides 
the fact sometime is a coroutine.

- Instead of allowing a coroutine in a function that is not a 
coroutine itself - and injecting a blocking call - I would 
require explicit call to evaluate the coroutine instead. No magic.

- Can coroutines be continued on another thread, assuming 
sequential consistency?


More information about the dip.development mailing list