3 ms·
And yet this proposal still doesn't let you implement zero-overhead objects-as-coroutines. I'd like to be able to create a coroutine that initializes some state
by kbwt 8y ago
And yet this proposal still doesn't let you implement zero-overhead objects-as-coroutines. I'd like to be able to create a coroutine that initializes some state ("constructor"), yields back to the caller, and lets you call alternate re-entry points ("member functions") that manipulate the state and yield back, or the final entry point that cleans up the state ("destructor"). If the initialization fails for whatever reason, the yield will simply be skipped. Crucially, I want to be able to do all of this without indirect jumps other than returns.
- AndyKelley 8y agoSerious question, what's the difference between this and a class?
- BeeOnRope 8y agoI guess when you call an entry point the second time it restores the existing context at the point of the previous yield. So you have mix of shared state (class members) and local state between the various entry point. Just a guess though.
- AndyKelley 8y agoIf the local state was accessed across a suspend point that would be undefined behavior. So it would have to be spilled into the coroutine frame. So it would be the same as a class.
- kbwt 8y agoIf you try to implement a class in C, you always end up with duplicated cleanup code between your "constructor" and your "destructor". Being able to jump from the constructor to the corresponding part of the destructor when the construction of a subobject fails would get rid of this often buggy/untested secondary code path. Some idioms have been developed over time, such as first initializing in a failsafe manner all the object's fields to a value that prevents deinitialization, but they add complexity to the code where it is not needed.