Replies: 6 comments 38 replies
-
I believe we could fix several (all?) of these issues with a syntax such as: loop_name: for <in, copy, out, inout, move or forward> <current_element> : <some_range> {
// body with mandatory curly braces
} Indeed, this syntax:
Moreover, introducing an for i : index_range(start, stop) next i++ { // next clause is useless here, just there to highlight consistency
// body with mandatory curly braces
} |
Beta Was this translation helpful? Give feedback.
-
BTW, I don't think this thread got to a loop syntax question I actually wrestled with more (sorry if I missed it), which is the location of For For the other loops, the So I ended up feeling okay about |
Beta Was this translation helpful? Give feedback.
-
BTW, how does the do
if filter_condition {
// body
} while stop_condition; And what is the behavior if the first iteration does not pass the filter? |
Beta Was this translation helpful? Give feedback.
-
I'm not convinced it's desirable to unify To me, the main benefit of a In terms of syntax, I do prefer If you scan all the I'm a big fan of Instead, I think we should be comparing the C++ Regarding Finally, I've been meaning to open a suggestion for this, but since it's on topic... I'd really love half-open and closed ranges (as in, numeric intervals) support for Cpp2 for 0..100 do (i: int) {} // Today's syntax
for (i: int) in 0..100 {} // Alternate |
Beta Was this translation helpful? Give feedback.
-
Musing about this a bit more, I want to call out something from one of my other comments. We're discussing unifying loop syntax, so can we find a syntax that fits all of them? The difference between Conceptually, if you haven't already learned the difference between
|
Beta Was this translation helpful? Give feedback.
-
Some idea I had this morning while walking to work: if starting with something that already exists is important for toolability, why not go all the way and spell the traversal of a range as: my_range.for_each( :( i: int ) = {
// body
} ); It should be easy to call under the hood |
Beta Was this translation helpful? Give feedback.
-
EDIT:
After a lengthy discussion with @MaxSagebaum and @hsutter, I realized that the main point I was trying to articulate is that we are very close to having a general unified syntax for control flow, which would naturally yield the usual
for
,while
andif
as special cases. The most natural syntax I have managed to come up with is:The only modification proposed here is the spelling of the third line. This particular proposition was motivated by the following arguments:
next
clauseThe main argument against this specific proposition is that introducing the element before the range breaks the "always start with something that already exists" principle, which reduces toolability. Maybe we can find a better proposition which would not have this drawback. What do you think?
ORIGINAL COMMENT:
Hi folks!
Here are some concerns about the current design of for loops, following the discussion in #1063. For loops in cpp2 are built as follows:
The
loop_name:
prefixed label and thenext
incrementation clause are optional. In the case of single-statement bodies, the curly braces are too. This syntax is very closely related to the function syntax, as for loops are essentially built asfor <range> do <lambda_function>
. The rationale behind this functional design is rooted in the fact that most for loops can be written as the traversal of a range. This is the motivation for the range-based for loop syntax in cpp1, which is a safer alternative to the traditional C syntax and more directly expresses intent:Note however that the cpp1 version reads left to right ("for current_element in some_range"), while the cpp2 version does not. Note also that a
do
keyword is added in the cpp2 version which:do
keyword connotes ado while
loopNon range-based cpp2 for loops are spelled as follows:
Note that the syntax is isomorphic to the C for loop syntax:
Note also that the
for
anddo
keyword are absent of this second flavor of for loops, as this for loop has become awhile
loop in cpp2. This implies that the curly braces are now mandatory, even for single-statement loops. Finally, note the inconsistent position of thenext
clause between the two kinds offor
loops.Beta Was this translation helpful? Give feedback.
All reactions