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

Michel Fortin michel.fortin at michelf.ca
Sat Dec 12 12:30:24 CST 2015


Le 12 déc. 2015 à 13:22, Rainer Brockerhoff <rainer at brockerhoff.net> a écrit :

> On 12/12/15 15:00, Michel Fortin wrote:
>> Le 12 déc. 2015 à 9:41, Rainer Brockerhoff via swift-evolution <swift-evolution at swift.org> a écrit :
>> 
>>> 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.
>> 
>> Are you sure this does what you expect?
> 
> Apparently Swift 2.x isn't properly in my brain yet, should've added
> `terminator:""` to those prints. Or thought of a better example.

True. But that's not what I meant here. Your example produces the same result as this one:

	for item in sequence {
		print("\(item)")
		print(", ")
	}

Since there is no `continue` in the loop body, there's no point in having a `repeat` block.

My guess is that your intent was to have ", " between each item, but not after the last one. That's not how the increment block works in a C-style for loop. Try this loop if you want to convince yourself:

	for var i = 0; i < 5; i += 1, print(", ", terminator: "") {
		print(i, terminator: "")
	}

Prints:

	0, 1, 2, 3, 4,

-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca



More information about the swift-evolution mailing list