[swift-evolution] C-style For Loops
Tyler Cloutier
cloutiertyler at aol.com
Sat Dec 5 13:53:35 CST 2015
Maybe a keyword like "oncontinue" would be a little more obvious about its behavior than "defer"?
> On Dec 5, 2015, at 5:17 AM, Michel Fortin <michel.fortin at michelf.ca> wrote:
>
>> Le 5 déc. 2015 à 2:54, Roland King <rols at rols.org> a écrit :
>>
>> Convert that to a while(), if you have a continue in the body, you have a good chance of not incrementing at all, or duplicating the increment code before every continue.
>
> Well... you can do that:
>
> var x = 0
> while x < 10 {
> defer { x += 1 } // warning: also increment x if an exception is thrown
> continue
> }
>
> Or maybe the C-style for loop could just be made more readable. You could replace the semicolons with words:
>
> for var x = 0 while x < 10 defer x += 1 {
> }
>
> But that might be slightly misleading. "defer" would imply that this part is executed when an exception is thrown, which would be unlike a regular for loop.
>
> --
> Michel Fortin
> michel.fortin at michelf.ca
> https://michelf.ca
>
More information about the swift-evolution
mailing list