yield functionality to match that of await

I was wandering if there are any issues in making `yield` usage to be the same as that of `await` Most importantly: l.append(yield Object()) Nice to have: yield with Object(): yield for i in Object(): Also, could anyone point me in the direction, where it is explained why new await/async syntax was introduced instead of making use of already existing yield coroutine functionality?

On Tue, 13 Jun 2023 at 07:33, Dom Grigonis <dom.grigonis@gmail.com> wrote:
You should be able to use that, since yield *is* an expression. The only oddity is, you have to parenthesize the yield expression again even though it's inside parentheses: l.append((yield Object()))
These, though, are unlikely to be supported, since "yield <expression>" isn't going to support statements.
Also, could anyone point me in the direction, where it is explained why new await/async syntax was introduced instead of making use of already existing yield coroutine functionality?
I don't know if there's a full explanation written down, but one important reason is that you can refactor async functions without worrying about suddenly changing their behaviour unintentionally. If you happen to refactor out the last "yield" from a generator, suddenly it's not a generator any more, and you'll have a weird time trying to figure out what happened; but an async function is an async function even if it doesn't (currently) have any await points in it. Another reason is that you can have asynchronous generator functions, which use both await AND yield. It's not easy to create a two-layer yielding system on top of yield alone. ChrisA

On 13/06/23 9:29 am, Dom Grigonis wrote:
Also, could anyone point me in the direction, where it is explained why new await/async syntax was introduced instead of making use of already existing yield coroutine functionality?
To my mind, the fact that coroutines use the same underlying mechanism as generators is an implementation detail. It's only like that for historical reasons, and it could change in the future. -- Greg

On Tue, 13 Jun 2023 at 09:33, Greg Ewing <gcewing@snap.net.nz> wrote:
I think they currently use what's basically a copy of the generator implementation. It makes sense to make resumable functions that way. (Fun fact: Pike looked at what Python was doing, and came up with a concept of "continue functions" which work broadly the same way that generators do, but with a declaration - kinda on par with writing "continue def gen():" in Python. Yes, it reuses the "continue" keyword.) ChrisA

On 13/06/23 11:38 am, Chris Angelico wrote:
I think they currently use what's basically a copy of the generator implementation. It makes sense to make resumable functions that way.
In many ways it does, although things get a bit messy when it comes to async generators. If I were designing something from scratch to support both, I would probably do it a bit differently.
(Fun fact: Pike looked at what Python was doing, and came up with a concept of "continue functions"
And I gather that the "async" and "await" keywords came from C#. Languages are always stealing from each other. :-) -- Greg

On Tue, 13 Jun 2023 at 14:54, Greg Ewing <gcewing@snap.net.nz> wrote:
Yeah, it's very common to borrow ideas like that. I just find it rather neat that Pike reused an existing keyword rather than creating a new one - Python had the same dilemma and chose to create a weird context-dependent keyword for a few versions, most other languages just made it a keyword and that's that, but Pike chose to go "well, let's call these things 'continue functions', and then we can use that word!". ChrisA

On Tue, 13 Jun 2023 at 07:33, Dom Grigonis <dom.grigonis@gmail.com> wrote:
You should be able to use that, since yield *is* an expression. The only oddity is, you have to parenthesize the yield expression again even though it's inside parentheses: l.append((yield Object()))
These, though, are unlikely to be supported, since "yield <expression>" isn't going to support statements.
Also, could anyone point me in the direction, where it is explained why new await/async syntax was introduced instead of making use of already existing yield coroutine functionality?
I don't know if there's a full explanation written down, but one important reason is that you can refactor async functions without worrying about suddenly changing their behaviour unintentionally. If you happen to refactor out the last "yield" from a generator, suddenly it's not a generator any more, and you'll have a weird time trying to figure out what happened; but an async function is an async function even if it doesn't (currently) have any await points in it. Another reason is that you can have asynchronous generator functions, which use both await AND yield. It's not easy to create a two-layer yielding system on top of yield alone. ChrisA

On 13/06/23 9:29 am, Dom Grigonis wrote:
Also, could anyone point me in the direction, where it is explained why new await/async syntax was introduced instead of making use of already existing yield coroutine functionality?
To my mind, the fact that coroutines use the same underlying mechanism as generators is an implementation detail. It's only like that for historical reasons, and it could change in the future. -- Greg

On Tue, 13 Jun 2023 at 09:33, Greg Ewing <gcewing@snap.net.nz> wrote:
I think they currently use what's basically a copy of the generator implementation. It makes sense to make resumable functions that way. (Fun fact: Pike looked at what Python was doing, and came up with a concept of "continue functions" which work broadly the same way that generators do, but with a declaration - kinda on par with writing "continue def gen():" in Python. Yes, it reuses the "continue" keyword.) ChrisA

On 13/06/23 11:38 am, Chris Angelico wrote:
I think they currently use what's basically a copy of the generator implementation. It makes sense to make resumable functions that way.
In many ways it does, although things get a bit messy when it comes to async generators. If I were designing something from scratch to support both, I would probably do it a bit differently.
(Fun fact: Pike looked at what Python was doing, and came up with a concept of "continue functions"
And I gather that the "async" and "await" keywords came from C#. Languages are always stealing from each other. :-) -- Greg

On Tue, 13 Jun 2023 at 14:54, Greg Ewing <gcewing@snap.net.nz> wrote:
Yeah, it's very common to borrow ideas like that. I just find it rather neat that Pike reused an existing keyword rather than creating a new one - Python had the same dilemma and chose to create a weird context-dependent keyword for a few versions, most other languages just made it a keyword and that's that, but Pike chose to go "well, let's call these things 'continue functions', and then we can use that word!". ChrisA
participants (3)
-
Chris Angelico
-
Dom Grigonis
-
Greg Ewing