[swift-evolution] Review: SE-0007 (Remove C-style for-loops with conditions and incrementers)
thorsten at portableinnovations.de
thorsten at portableinnovations.de
Thu Dec 10 01:22:51 CST 2015
1. Evaluation
Initially I was hesitant but now I support this change.
The C-style for-loop is syntactic baggage (the semicolons do not separate its parts very well) and suffers from the problem that it is imperative, i.e. The loop variable is just that, a variable, and therefore will not work as expected when captured in closures that are executed later, e.g. in a GCD queue.
There might be some corner cases where the C-style for-loop cannot be easily replaced with a for-loop or a while-loop in the presence of several continue statements, which is why I originally proposed to Swiftify the C-style for-loop instead of removing it, but I haven't seen a concrete one yet and I am optimistic that good solutions can be found even for these cases by refactorings e.g. extracting the loop body into a function and replacing continue with return. If someone has a concrete case for which that does not apply for some reasons I'd be interested to see that case and reconsider.
2. Significance
The C-style for-loop does not fit syntactically and its use is limited. In most cases the for-in loop is much better suited and more expressive and for the remaining few cases a while-loop can be used.
3. Direction of Swift
This proposal fits the philosophy of Swift with regards to readability and safety because the alternatives are more expressive and readable.
4. Comparisons
I have long experience with mostly Smalltalk, Haskell, Swift and Java, and from earlier times with C, C++, Python and Ruby. Furthermore I follow the development of many languages with modern constructs closely, e.g. Scala, Ceylon, Clojure and others and have a strong interest in programming language design.
Most of these languages, i.e. all except Swift, Java and of course C and C++, do not have a C-style for-loop. They typically do have more expressive loops similar to the for-in loop of Swift (or more powerful, e.g. Scala's for-expression which is similar to Haskells monadic do-notation).
Dylan has an iterating for-loop which improves on the C-style for-loop by introducing a new binding for the loop "variable" on each iteration, which might look like follows in a Swiftified syntax:
for i = 0 then i + 1 while i < limit { ... }
for i = 0 then i + 1 until i == limit { ... }
which effectively introduces a let binding in the init clause scoped to the loop, an expression to replace the binding with for the next iteration and a while or until clause which is checked before each iteration.
I would propose this as an alternative to removing the C-style for-loop if a review makes a convincing case that an incremental loop is still needed.
5. Effort
I read the proposal carefully, read the discussion in the mailing list, checked two large Java codebases (large systems for logistics) for occurences of for-loops and did only find trivial ones. Furthermore I checked textbooks for their looping constructs, e.g. "Concepts, techniques and models of computer programming" by van Roy and Haridi (highly recommended!), .
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151210/76fffbb8/attachment.html>
More information about the swift-evolution
mailing list