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

Matthew Johnson matthew at anandabits.com
Sat Dec 12 09:44:26 CST 2015


> 
> So, I propose that the `while` loop should get an optional `repeat`
> clause at the end, like this:
> 
> var item = firstItem		// declaration/initialization
> while item != nil {	// condition
> 	if SomeExtraCondition(item) {
> 		break;
> 	}
> 	process(item)
> } repeat {			// increment
> 	item = NextItemFor(item)
> }
> 
> Where the statements inside `repeat` are executed after each loop, but
> only if the loop will continue. I think this offers the necessary
> conceptual clearness and existing for-loops are easily converted
> automatically. The eye will skip over the `repeat` clause for the last
> iteration as it now does for `else`.
> 
> Also, `break`, `continue` and `throw` will all continue to work as expected.
> 
> I also propose that the `repeat` clause be extended to the remaining
> `for-in` loop:
> 
> for item in sequence {
> 	print("\(item)")
> } repeat {
> 	print(", ")
> }
> 
> which will be handy in many situations.


I haven’t had time to do a thorough review of the proposal myself but I have read the proposal and have been following the responses.  My impression is that most people who don’t support this proposal would do so if there was an acceptable alternative which retains the performance characteristics of the C-style for loop.  

Rainer’s proposal looks like a good way to provide an alternative that addresses semantic and performance concerns while also providing a tool that generalizes to all loops.  It feels “Swifty” and seems like a win to me.  

I can’t think of any reason to oppose the proposal with Ranier’s solution other than old habits, familiarity from other languages, etc.

I do think we need to consider whether “repeat” is the best keyword due to potential confusion with "repeat while” loops.  The advantage of using it is avoiding adding a new keyword.  The disadvantage is overloading its meaning.

Matthew


More information about the swift-evolution mailing list