[swift-evolution] Review for: Remove C-style for-loops with conditions and incrementers

Greg Parker gparker at apple.com
Wed Dec 9 01:04:34 CST 2015


> On Dec 7, 2015, at 5:29 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
> 
> var i = initial
> while cond(i) {
>    defer { i = increment(i) }

Strange. Can the compiler optimizer handle that efficiently?


> Michel Fortin wrote:
> 
> var i = 0
> var first = true
> while ({ () -> Bool in if first { first = false } else { i = increment(i) }; return cond(i) }()) {
> }

I fear that teaching the C-style for loop is easier than teaching that.


The C-style for loop has the great advantage that it can express a large number of loop constructs concisely. Most or all of the loop logic is collected in one place. It is also familiar to many developers via other languages.

Proposal:
1. Keep the C-style for loop.
2. Discourage use of the C-style for loop in simple cases with a compiler warning and fix-it to a for..in loop. This would be analogous to the existing warning of "variable was never mutated; consider changing to 'let' constant".


-- 
Greg Parker     gparker at apple.com     Runtime Wrangler




More information about the swift-evolution mailing list