> the removal of traditional for loops […] When I saw it, my reaction was ... WHY???
For all of these reasons, at least: https://github.com/apple/swift-evolution/blob/master/proposa...
For all of these reasons, at least: https://github.com/apple/swift-evolution/blob/master/proposa...
C style for loops come pretty early in most programming tutorials, but I wonder how much non-C programming does actually use them nowadays (from the Community Responses, it seems not much Swift courtesy of other options). Usually, a C style for would be to loop over an array, and a safer way to do that probably could have stopped countless vulnerabilities & bugs occurring over the years.
Meh. Many languages don't have c-style for loops in the first place. Neither Python nor Ruby do for instance. I don't think Rust ever had them either[0].
[0] https://www.reddit.com/r/rust/comments/2957fg/can_i_request_...
for thing in collection: process(thing)
and very easy to move from that to a functional style: map(collection, process)
or collection.map(process)
The number of times I also need a integer counter is fairly small. for (index, element) in collection.enumerated()
There are some cases where the C style for loop is the most natural way to express something. For example, looping over NULL-terminated array of pointers is nicely expressed with one (Swift 2-ish pseudocode): for var cursor = ptr; cursor.pointee != nil; cursor += 1
But these situations are really rare, and when you do encounter them, it's not a big deal to transform them into a while loop: var cursor = ptr
while cursor.pointee != nil {
defer { cursor += 1 }
...do stuff...
} result := collection collect:[ :a | a process ].
result := collection collect process.
collection do process.
1 to: 10 do: [ :i | stdout println:i ].
stdout do println: (1 to: 10).
All just plain messages and plain message syntax, no special control structures needed.