Bug 16272 – Yield like semantics for function execution
Status
RESOLVED
Resolution
MOVED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2016-07-12T21:42:32Z
Last change time
2022-11-09T15:19:57Z
Assigned to
No Owner
Creator
TeddyBear12311
Comments
Comment #0 by TeddyBear12311 — 2016-07-12T21:42:32Z
In some cases it would be nice to break the execution flow of a function and re-continue at a later time. This reduces bloat and complexity.
int A(int i)
{
...
?yield;
...
?yield;
...
?yield;
...
return 0;
}
void B()
{
int x = 3;
auto uuid = yield A(ref x) // Calls A but activates ?yields
//returns from A after first ?yield
...
x = 6;
continue!uuid(A(ref x)) // continues
// returns from A after second ?yield
...
continue!uuid(A(ref x));
// returns from A after third ?yield
...
break!uud(A());
// breaks out of A as it A executed a return statement. No return value is given.
}
All inputs to A are by ref when yielding so they can be modified. The break is optional but allows one to terminate execution of the function. Every continue must match a ?yield up to a break.
uuid's are simply ways to identify different function execution strains.
Does this allow for total chaos if used wrong? Yes! It's more valuable to create simple and complex state machines very effectively with minimal work and better understanding of the complexity. Each ?yield statement can be seen as a pause in the state machine between transitions.
Comment #2 by TeddyBear12311 — 2016-07-12T22:53:34Z
(In reply to Mathias Lang from comment #1)
> This is already possible using a library solution, see
> https://dlang.org/phobos/std_concurrency.html#.Generator
I believe their is a subtle difference. I could be mistaken because I don't know the depth of fibers.
Fibers are more concurrency related and imitate a task switch. In this case with functions, no task switching like behavior is needed. Just a push and pop of the locals and a few other little things. It may be exactly what a fiber does though?
I'm thinking it is more like thread(heavy) > fiber(medium) > functional yield(light weight).
Comment #3 by razvan.nitu1305 — 2022-11-09T15:19:57Z
This would require a DIP, however I doubt that it is going to pass through. As pointed out, fibers do this exactly.
I am going to close this. If you want to pursue it [email protected], please submit a DIP.