Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Going Towards a More General Looping Mechanism - this should be a serious proposal. #13

Open
robertramey opened this issue Jun 17, 2020 · 0 comments

Comments

@robertramey
Copy link

Going Towards a More General Looping Mechanism

Document number: DXXXXR0
Date: 20XX-MM-DD
Audience: EWG
Reply-to: Tony Van Eerd. tt at forecode.com

Summary

Current looping mechanisms (forloops, while and do...while loops)
are great - in their niche uses;
but they are often found wanting, resulting in awkward constructs.
In particular, in a few cases it is impossible to NOT repeat yourself (ie a violation of DRY) or to be concise, using those mechanisms.

Looking at how all these mechanisms work, we can refactor them into a single, more general, control flow mechanism.
In fact, it could replace all the other keywords with this single mechanism!

(Note, for the sake of backwards compatibility, we do not recommend deprecation at this time.
If the new style is widely adopted - as we expect it will - we can deprecate the old keywords
when common coding practices show they have fallen out of favour.)

Extra Checks DRY and Concise
bool res = false;

for(int retry = 10;  !res && retry;  retry--)
{
    res = askHardware(question, &num);
    // don't know why it succeeds while failing,
    // but it does.  :-(
    // Sadly, re-asking is the solution.
    if (res && num == 0) {
        sleepHack(200);
        continue;
    }
}
return res;

// pre-conditions
bool res; // undetermined
int retry = 10;

// invariant
// retry >= 0

// post-conditions
auto post_condition = bool [res, num, retry](){
  res && num == 0 || retry == 0 
}
do{
  int num;
  sleepHack(200);
  res = askHardware(question, &num);
  --retry; // advances state - must terminate
}
while(! post_condition());
return res;

More Elucidating Examples

... show more problematic uses of current control flow, compared against new mechanism

Proof of Replacement

... show before/after table replacing each of { for, while and do...while }

Technical Details or Preliminary Wording, Etc

Label statements, goto statement. Etc.

Further Work

Initial results suggest a "computed goto" could also replace if and switch statements - thus completely replacing all control flow mechanisms with a single keyword. The committee should give me a kidney.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant