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

thorsten at portableinnovations.de thorsten at portableinnovations.de
Tue Dec 8 09:51:44 CST 2015


As the removal of the c-style for-loop is supposed to make things easier to understand I'm a bit worried about the replacements for corner cases of the c-style for-loop.

Defer is simply wrong and dangerous and putting the increment into the condition is even uglier than the c-style for-loop 😕

Because of that I'd like to repeat my proposal of a modernized syntax for the c-style for-loop which gets rid of the c-style and might be preferable for those corner cases.

for var i=0 while i < limit next i += 1 { ... }

together with its companion

for var i=0 until i == limit next i += 1 { ... }

-Thorsten

> Am 08.12.2015 um 03:04 schrieb Kevin Lundberg via swift-evolution <swift-evolution at swift.org>:
> 
> For throw, if incrementing i has no external side effects, then there shouldn’t be any issue since i will be thrown away anyways when the error is thrown. 
> 
> For break, it can be considered dangerous, but if your while loops are written semantically like the old c-style for loop, the counter shouldn’t be used outside the scope of the loop anyways. If that matters to whomever may write code like this, then manual incrementing at each point where loop scope breaks is necessary (though I imagine such cases are likely rare).
> --
> Kevin Lundberg
> 
>> On Dec 7, 2015, at 8:58 PM, Michel Fortin via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Le 7 déc. 2015 à 20:29, Kevin Ballard via swift-evolution <swift-evolution at swift.org> a écrit :
>> 
>>> On Mon, Dec 7, 2015, at 01:07 PM, Dmitri Gribenko via swift-evolution
>>> wrote:
>>>> Another concern of mine is the equivalent of C-style for loops with
>>>> 'continue' statements in them.  The only equivalent based on 'while' I
>>>> can think of duplicates the increment portion of the loop.
>>> 
>>> As has been pointed out in other threads on this topic, you can use a
>>> defer statement to avoid duplicating the increment portion:
>>> 
>>> var i = initial
>>> while cond(i) {
>>>   defer { i = increment(i) }
>>>   // ...
>>>   if frob {
>>>       continue // defer will execute
>>>   }
>>> }
>> 
>> Which is a dangerous substitute, because it also does the wrong thing with break and throw:
>> 
>> 	var i = initial
>> 	while cond(i) {
>> 		defer { i = increment(i) }
>> 		// ...
>> 		if frob {
>> 			continue // defer will execute
>> 		} else if blob {
>> 			break // defer will execute!!!
>> 		} else if plob {
>> 			throw MyError // defer will execute!!!
>> 		}
>> 	}
>> 
>> 
>> -- 
>> 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
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151208/339b5a65/attachment.html>


More information about the swift-evolution mailing list