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

David Waite david at alkaline-solutions.com
Fri Dec 11 05:20:28 CST 2015

This might be a controversial opinion, but:

- The indices/values being created by a for loop should be immutable through each iteration of the actual loop (this is actually enforced by for-in and forEach() ). For example, this is appropriate for iterating a sequence
- If you are modifying indices within the body of a loop, you should use while or repeat..while. This is appropriate for use cases like traversal of a singly-linked list.

We have seen plenty of for statements that fit each bucket, so there isn’t a single fix-it option. In fact, I don’t believe it is appropriate to have a fixit option more complex than “it looks like you meant to use a range”. We will not be able to interpret the purpose of the loop, and it will be easier for developers to recognize the meaning of the code they wrote vs our generated equivalent.

Defer might be appropriate in some cases. It might not be appropriate in others. Why are we trying to guess the programmer’s intentions?


> On Dec 11, 2015, at 12:55 AM, thorsten at portableinnovations.de wrote:
> I revoked my support for two reasons: One, worries about performance which has in the meantime been confirmed by David Owens II to suffer considerably, and Two, by repeated suggestions of using defer for the incrementing clause even though it changes semantics. 
> Removing the for-loop would therefore mean that we lose a construct needed in cases whe high performance is requird and that instead of incrasing readability we would decrease it in those cases where defer would be misused.
> -Thorsten

More information about the swift-evolution mailing list