Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upCoroutines #53
Conversation
eddyb
reviewed
Apr 25, 2014
|
|
||
| # Summary | ||
|
|
||
| Add "shallow" coroutines similar to Python's generators. |
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 25, 2014
Member
@thestinger confused me a bit about the subject, but my last findings suggest Python is misusing the word "coroutines", which traditionally are a superset of "generators" (the difference being the ability to yield through multiple stack frames).
This comment has been minimized.
This comment has been minimized.
eddyb
reviewed
Apr 25, 2014
| let mut result = coroutine(a1, a2, a3); | ||
| while result.is_yield() { | ||
| // ... process result | ||
| result = coroutine(b1, b2, b3); |
This comment has been minimized.
This comment has been minimized.
eddyb
Apr 25, 2014
Member
Can't we just extend Iterator<T> instead?
Since generators that don't get input from yield are actually iterators.
This comment has been minimized.
This comment has been minimized.
|
I would much rather see all the tools required for implementing this (in a syntax extension, for example), than baking it into the language (like closures are today, and will be before you can implement |
This comment has been minimized.
This comment has been minimized.
|
I think you'd need some kind of "unsafe goto", which is ignored by borrow/lifetime checker passes for this to be possible as a syntax extension. Not likely, in my estimation... |
This comment has been minimized.
This comment has been minimized.
|
Why? When you can do a state machine transform and you get code that's easier to optimize. |
This comment has been minimized.
This comment has been minimized.
|
@eddyb, I would love to see how that transform looks like! |
This comment has been minimized.
This comment has been minimized.
eevee
commented
May 4, 2014
|
Based on my experience with Python:
|
This comment has been minimized.
This comment has been minimized.
Assuming the coroutine->iterator "adapter" trait is provided by the standard library (as it should), your collection iterator implementation will look like this:
We could support that, but what would that save us vs the above? A pair of braces? (presumably, you'd still have to say
Quite the opposite, actually. This is where being wrapped in an outer function comes in handy: The initial set of arguments go to the outer function, which may perform some pre-processing, before constructing and returning the coroutine closure. |
vadimcn
changed the title
Coroutines RFC
Coroutines
May 5, 2014
This comment has been minimized.
This comment has been minimized.
|
Thanks for the RFC! The team has decided that while desirable, this is not needed for 1.0 at this time. I imagine that a library such as this will work best with as little language support as possible, so this may be best done as a new library rather than a language change per-se. Closing. |
alexcrichton
closed this
Jun 12, 2014
This comment has been minimized.
This comment has been minimized.
|
:( Did the team have concrete suggestions of how this could be done as a library? |
This comment has been minimized.
This comment has been minimized.
|
We believe that it's possible to add support for this in a backwards-compatible fashion, especially because |
This comment has been minimized.
This comment has been minimized.
flying-sheep
commented
Jul 9, 2014
|
how should that be possible?
this needs first-class support of pauseable and resumable code. |
This comment has been minimized.
This comment has been minimized.
|
I think what Alex means is that a library in stdlib could provide the functionality. This is analogous to how (Caveat: I have not given any consideration to how the borrow-checking rules could be impacted by such a feature. I am assuming it's a solvable problem at worst.) |
This comment has been minimized.
This comment has been minimized.
flying-sheep
commented
Jul 10, 2014
|
ok, this is sadly far over my head. the implications and techniques necessary to create a data structure wrapping a function’s environment and stack is too deep to consider working on. i hope some heavy contributor takes this up. |
vadimcn commentedApr 25, 2014
Rendered