Related bug report #3417. Submitting a feature request since the bug is valid on its own.
while loops should be added to the language, and the equivalent usage of for removed.
Using for as the only looping construct leads to unclear code when trying to write a traditional while loop using the in operator to test for membership.
The following example attempts to iterate over the array, which is the common usage of for ... in:
for x in [1, 2, 3] {
}
If instead you wanted to loop until x was not 1, 2, or 3 you'd have to write one of the following:
for (x in [1, 2, 3]) {
}
for ;x in [1, 2, 3]; {
}
Neither of these options look significantly different from the first example, which makes it hard to recognize what's happening.
In other languages, this would be written more naturally as:
while x in [1, 2, 3] {
}
The use of in to test membership and for ... in creates the confusion. Adding while to the language clears everything up, reads more naturally, and is familiar to almost all programmers.
I agree, I knew of this for a while. Most likely this will be solved with a while loop.
while loops should be added to the language, and the equivalent usage of for removed.
I have added support for while loops in #3609. However, I haven't removed the equivalent usage of for loop as it will break a lot of existing code.
A warning is being printed (which, for some reason is not appearing for me though), that combined with some way of deprecating the for loop usage so as to migrate everything slowly to use while loop will help us in removing for condition { syntax from the language.
Copy pasting my response:
while x in array needs an x variable already defined, and the for loop declares a new variable.
So if the programmer tries to do
x := ...
for x in array { // while x in array
}
The program won't compile because x is redeclared.
while x in array { is used very rarely, and can be achieved with for (x in array) { or for { if !(x in array) { break } }
Most helpful comment
I agree, I knew of this for a while. Most likely this will be solved with a
whileloop.