[Proposal]: switch for
Duff's Device
#3922
Replies: 4 comments 4 replies
-
Can switch (i)
{
case 0: goto pos0;
case 1: goto pos1;
case 2: goto pos2;
case 3: goto pos3;
case 4: goto pos4;
case 5: goto pos5;
case 6: goto pos6;
case 7: goto pos7;
}
pos0:
;
pos1:
;
pos2:
;
pos3:
;
pos4:
;
pos5:
;
pos6:
;
pos7:
; |
Beta Was this translation helpful? Give feedback.
-
In the future, please submit this type of proposal as a discussion, rather than an issue. We would like to reserve new issues for fully-specified proposals that have had discussion with members of the team and the community about the benefits and drawbacks, and the format of issues does not make it easy to have such a discussion. |
Beta Was this translation helpful? Give feedback.
-
The wish for something like "start in the middle" of a loop occurred to me as well, but not in a frequent or bothering way. I think what I usually do is one of the following: while (true) {
some_code_X;
if (!condition) {
break; // if you can live with a break (I usually cannot)
}
some_other_code;
} do {
some_code_X;
if (condition) {
some_other_code;
}
} while (condition) // if your condition is side-effect free (which I strongly recommend anyway) TLDR: Don't DRY. If all else fails, at least extract a (possibly local) method for the repeated code block - and then why not use that opportunity to give it meaningful name. |
Beta Was this translation helpful? Give feedback.
-
More specifically, jumping into the middle of a loop creates irreducible control flow graphs https://en.wikipedia.org/wiki/Control-flow_graph which makes it hard for a compiler to optimize (and why gotos got a bad name) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Duff's Device
Summary
[summary]: Implement the Duff's Device as a language feature.
A Duff's Device allows entry into a loop partway into the loop. Duff's Device relies on fall-through case switching.
Motivation
[motivation]: Keeps happening
It's infrequent, but from time to time I find myself writing code in the form:
These are conditions where
some_code_X
has to execute in the initial state beforesome_other_code
is valid; butsome_other_code
is not valid after the final execution ofsome_code_X
, therefor no ordering of the code inside a loop works. The answer is to violate the DRY principle, copying the same code twice and maintaining both bodies of code when they change.These chicken-and-egg problems are frequent enough to be a memorable annoyance, but not so frequent as to draw real-world examples to mind easily. That's the sweet spot where a problem gets ignored even if it's a problem.
Detailed design
[design]: Just a Duff's Device
This would involve some manner of switching into a
for
,foreach
,while
, ordo
loop based on a condition at entry. No further evaluation would be carried out: at the loop's entry point, the entry condition is evaluated, and a case label is selected as the entry point of the first iteration only.Drawbacks
It would be perhaps problematic, perhaps not, to modify the general
switch
statement to automatically fall through (as in C), much less to allow it to envelope a loop. It would also be odd and cluttered code.This means a new type of
switch
would be necessary, which may then cause confusion about the old type ofswitch
. Within the device, thebreak
command is not valid in terms of theswitch
:break
exits the loop, which is the immediate scope.Alternatives
A new keyword prefixed to a loop is an alternative. Possibilities are
entry
,from
, orduff
(after the original Duff's Device). The form would be:The keyword would need to precede the
for
,foreach
,do
, orwhile
statement, since a statement immediately following any of these is interpreted as the statement on which the execution is conditioned.Unresolved questions
It is perhaps nonsense to support this for both
do
andwhile
, asdo
is equivalent to a Duff's Device that switches into the first execution of awhile
loop without testing the condition.Beta Was this translation helpful? Give feedback.
All reactions