[swift-evolution] [Review] SE-0007 Remove C-style for-loops with conditions and incrementers
Brent Royal-Gordon
brent at architechies.com
Sat Dec 12 12:37:08 CST 2015
>> As for c-style-for vs while, the two are mechanically convertible.
>
> This is provably false and has been demonstrated, but here is an example of it again:
>
> var sum = 0
> for var i = 10 /* expr1 */; i > 0 /* expr2 */; i -= 1 /* expr3 */ {
> if i % 2 == 0 { continue } // statement
> sum += 1 // statement
> }
> print(sum)
>
>
> var sum = 0
> var i = 10 // expr1
> while i > 0 /* expr2 */ {
> if i % 2 == 0 { continue } // statement
> sum += 1 // statement
>
> i -= 1 // expr3
> }
> print(sum)
>
> It’s not just a mechanical conversion; consideration for early loop-exits and continuations need to be made as well. The rote conversion for the while version above is an infinite loop. Not only that, expr1 now leaks variables into a scope that is no longer contained within the loop.
You know, Perl *makes* it mechanically convertible by adding a continue {} block after the while {} loop. The continue {} block is run between the loop body and the condition, so it’s skipped by `break` (well, `last`) and other early constructs that leave the loop, but not `continue` (spelled `next` there). Maybe something along those lines would help us here by making the loss of `for` more palatable. (Although that does kind of defeat the purpose of removing this syntax…)
--
Brent Royal-Gordon
Architechies
More information about the swift-evolution
mailing list