evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.05k stars 120 forks source link

Add a single construct which both does `async with` and then `async for` #753

Closed evhub closed 1 year ago

evhub commented 1 year ago

When working with async generators, it is often advised, using something like aclosing or trio_async_generator to implement a pattern like:

async with my_generator() as values:
    async for x in values:
        ...

which unnecessarily creates two nested blocks in a relatively non-ergonomic way. Instead, we could have a single syntax like

async with for x in my_generator():
    ...

that compiles to the code above.