[swift-evolution] C-style For Loops

Michel Fortin michel.fortin at michelf.ca
Sun Dec 6 14:37:57 CST 2015

There is actually one good reason I see in favor of keeping the C-style for loop, and only one: ease of porting existing code to Swift.

If you are porting code from another language that uses C-style for loops, currently you can keep the loop as is while you translate all the syntactic differences everywhere. If Swift didn't had a C-style for loop, then you'd need to rethink the control flow at the same time you are converting everything else, increasing the risk of errors during what is already a delicate operation.

C-style for loops are very common in code you can find everywhere because many languages have it, and therefore there is a lot of algorithms, sometime tricky algorithms you don't want to mess up, that rely on such loops. Even if you had the desire to convert all of them to for-in loops in your port, rewriting the control flow *at the same time* you are addressing all the other porting issues is much worse than doing it in a separate step *after* the crude port has been tested to work.

In other words: you should avoid refactoring everything in one step. The C-style for loop enables that.

Michel Fortin
michel.fortin at michelf.ca

More information about the swift-evolution mailing list