C++ Logo

std-proposals

Advanced search

Re: Making coroutines more explicit.

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 21 Aug 2019 11:17:48 -0400
On Wed, Aug 21, 2019 at 11:11 AM Robert Kubok via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello,
> I have a proposal: make corotines more explicit in use, than it is
> proposed right now. The thing is: a function is treated as coroutine, when
> a 'co_yield' or 'co_return' keyword is used inside callable object's body.
> Whether a function is a coroutine or not, is determined by it's body, not
> by it's definition. In my opinion this is a language flaw. Therefore I
> suggest creating a new keyword: 'coroutine', which is going to be a
> type-specifier to a function or lambda expression and then, changing the
> upcoming keywords related to coroutines ('co_await', 'co_yield',
> 'co_return' to 'await', 'yield' and 'return' respectively). As always, it
> is better to see the syntax in action, so there's the example:
>
> coroutine auto iota(int n = 0)
> {
> while(true)
> yield n++;
> }
>
> would be equivalent to proposed:
>
> auto iota(int n = 0)
> {
> while(true)
> co_yield n++;
> }
>
> Or with lambda expression:
> auto getFive = []() coroutine // in the same place as 'constexpr' keyword
> {
> return 5;
> };
>
> which of course would be equivalent to:
> auto getFive = []()
> {
> co_return 5;
> }
>
> Why 'coroutine' keyword? Firstly, coroutine should be specified by it's
> *declaration*,
>

Nit: You mean "definition," not "declaration." Coroutines are already
designed so that from the outside a coroutine function looks exactly like a
subroutine function, and in fact can be implemented as either a coroutine
or a subroutine. The caller doesn't have to know or care. They just see a
function returning a foo::future or a foo::generator, which is awaitable;
how the innards of that future or generator work isn't relevant to the
caller.

Your proposal is basically identical with Antony Polukhin's P1485 "Better
keywords for coroutines."
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1485r1.html>
https://quuxplusone.github.io/blog/2019/06/26/pro-p1485/

Unfortunately, unless WG21 shapes up real fast, it's "too late" for any
such change to coroutines. You'd have to fork C++ to get a better syntax at
this point. :(

–Arthur

Received on 2019-08-21 10:20:02