[swift-evolution] C-style For Loops

Paul Cantrell cantrell at pobox.com
Sun Dec 6 20:41:55 CST 2015


People can do the (usually) trivial transformation to a while loop first. The cases where loop exits make the transformation nontrivial seem rare to me.

Doing a port of C, C++, Java, or C# code to Swift, that for→while transformation seems trivial next to, say, dealing with the differences in Swift’s type system, its memory management, or its handling of unsafe pointers — all things one would have to deal with in the first pass of a port.

I’m not sure easy porting from C is even a design consideration, but even if it is, it shouldn’t be a reason to hold back this proposal.

Cheers, P

> On Dec 6, 2015, at 2:37 PM, Michel Fortin via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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
> https://michelf.ca
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list