-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Proposal: do...while loops similar to the C++ #5781
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
base: trunk
Are you sure you want to change the base?
Conversation
FWIW for history on this, proposal #340 added Note this is also mentioned in #353, for why |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll definitely need to consider whether this is a direction we want to pursue. Some early feedback:
The concrete syntax do ... while (...)
has historically been problematic in C-family languages. It adds significant context-sensitivity to the while (...)
construct, making it mean something different if there's a preceding do ...
. Eg, if you see
...
}
while (cond);
in C++, it's unclear locally whether the while
attaches to a prior do
or not. This wouldn't be so bad in Carbon because braces are mandatory on nested statements, so we could distinguish the two based on whether the while
is followed by a ;
or a {
, but the reuse of the while
keyword and following syntax is still somewhat a concern.
More broadly, though, we should consider whether we want to address the "loop-and-a-half" use case as well as the "at least once" use case, plus any other similar loop modalities. (The loop-and-a-half case is something like:
vector<expr> exprs;
while (true) {
exprs.push_back(parse_expr);
if (next_token() != comma) break;
consume_token();
}
... where the loop condition isn't at the start or end of the loop but somewhere in the middle.) It'd be good to have some data on how common do...while
is compared to other loop modalities -- that'd inform whether it's worth having dedicated language syntax for this, including a new (2 letter) keyword, or whether we should pursue something broader, or just tell people who want do...while
to instead use
while (true) {
...
if (!cond) break;
}
@zygoloid Regarding putting an do {
...
if (cond1) continue;
...
} while (cond2); With while (true) {
...
if (cond1) {
if (!cond2) break;
continue;
}
...
if (!cond2) break;
} e.g., something we'd discussed in the past was: loop {
...
if (cond1) continue;
...
} on continue {
if (!cond2) break;
} Note that's also flexible in terms of "on break", etc. |
So I guess the generalization of these is: And the difference between this superset and something like C++'s (& mid-loop conditions makes me think of Duff's Device... ) It's like we need the opposite of an
mid-condition:
Mid-condition for:
The words aren't great - while C-family developers are already comfortable with the fact that the increment in a Should the prefix be thought of more as a tail instead? Is that equivalent?
do-while would be:
|
This proposal introduces do...while loops to Carbon, providing a post-test loop construct that executes the loop body at least once before checking the condition.
Key features:
Syntax:
do { statements } while (condition);
Executes loop body at least once, then evaluates condition
Supports break and continue statements
Addresses common patterns like input validation, menu systems, and retry mechanisms
Benefits:
Eliminates need for code duplication or while(true) workarounds
Improves code readability for post-test loop patterns
Maintains consistency with C-family language conventions
Enhances C++ migration compatibility
This complements Carbon's existing while and for loop constructs, providing developers with a complete set of loop control structures.