[swift-evolution] [Pitch] Retiring `where` from for-in loops

Dany St-Amant dsa.mls at icloud.com
Fri Jun 10 07:45:25 CDT 2016

> Le 10 juin 2016 à 02:25, Xiaodi Wu <xiaodi.wu at gmail.com> a écrit :
> * The word "where" does not consistently imply `break` or `continue`. In current Swift, `where` implies `break` in the context of a `while` loop and `continue` in the context of a `for` loop. Some users intuitively guess the correct meaning in each context, while others guess the wrong meaning. Therefore, the only way to learn for sure what `where` means in any context is to read the rulebook. That, by definition, means that this is unintuitive.

An example here would have help... I had trouble visualizing the 'where' with 'while'. A quick example I was able to conjure is:

var array =  [9,8,7,6,5,4,3,2,1]
while let x = are.popLast() where x < 5 { print(x) }

What? 'array' is not empty at the end. I admit, I was surprised by the result at first. The confusion here is not that 'where' in 'for' behave like 'continue' and 'where' in 'while' act like a 'break', as the later doesn't conceptually exist. The later concept is using 'where' as a conditional binding in a 'while' causes a failure of the bind and thus a 'break' out of the loop. With the acceptance of the revised SE-0099, the 'while' can no longer appear to be using 'where', thus removing this confusion.


More information about the swift-evolution mailing list