[swift-evolution] Add a while clause to for loops

Tim Vermeulen tvermeulen at me.com
Tue Jun 7 14:39:42 CDT 2016


> `while let element = sequence.next() where condition {…}`

This is not a good alternative. First of all, it uses `while let` and `next()` instead of `for … in` which is way more complex than necessary. Also, if we want to skip over some elements, we either have to use

`sequence.lazy.filter({ someCondition($0) }).next()`

which is very hard to read compared to the proposal, or

`guard someCondition(element) else { continue }`

which is certainly better, but the added noise is still not very elegant. It’s also slightly confusing because we’d be both evaluating a condition outside of the loop body and inside the body.

> `for element in sequence.lazy.filter({ condition }) {…}`

Not something I would go for either, again, because of the boilerplate code. You end up with an anonymous closure parameter despite already having named the element “element”, unless you name the parameter, but then you’d have to name it twice. Also, `.lazy.filter({ condition })` is a lot harder for beginners to grasp than a `where` or `while` keyword.

> And it can be explicitly spelled out inside the loop

Probably the best option for now. In my opinion it’s still a bit confusing to enter the loop body but then decide to leave anyways, but it’s not too bad.

> You're describing a while loop:
> `while let element = sequence.next() where condition {...}`
> 
> Which as we've discussed can already be re-written with a for loop (which, yes, can be lazy):
> `for element in sequence.lazy.filter({ condition }) {...}`
> 
> And it can be explicitly spelled out inside the loop, a definite readability gain for the same reason guard always requires an explicit else block.
> 
> What do you gain with a new keyword?
> 
> On Tue, Jun 7, 2016 at 05:02 Haravikk via swift-evolution<swift-evolution at swift.org(mailto:swift-evolution at swift.org)>wrote:
> > I’m a +1 for this idea. Like Thorsten I was initially a little concerned that while and where may look too similar, but actually I find them visually distinct enough, and actually in my code I’m probably more likely to use while than where on for loops, although both are useful.
> > 
> > >On 6 Jun 2016, at 11:15, Tim Vermeulen via swift-evolution<swift-evolution at swift.org(mailto:swift-evolution at swift.org)>wrote:
> > >
> > >We can already use a where clause in a for loop like this:
> > >
> > >for element in array where someCondition(element) {
> > >// …
> > >}
> > >
> > >which basically acts like
> > >
> > >for element in array {
> > >guard someCondition(element) else { continue }
> > >// …
> > >}
> > >
> > >Sometimes you want to break out of the loop when the condition isn’t met instead. I propose a while clause:
> > >
> > >for element in array while someCondition(element) {
> > >// …
> > >}
> > >
> > >which would be syntactic sugar for
> > >
> > >for element in array {
> > >guard someCondition(element) else { break }
> > >…
> > >}
> > >
> > >I can see this particularly being useful if we have a sorted array and we already know that once the condition isn’t met, it won’t be met either for subsequent elements. Another use case could be an infinite sequence that we want to cut off somewhere (which is simply not possible using a where clause).
> > >_______________________________________________
> > >swift-evolution mailing list
> > >swift-evolution at swift.org(mailto:swift-evolution at swift.org)
> > >https://lists.swift.org/mailman/listinfo/swift-evolution
> > 
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org(mailto:swift-evolution at swift.org)
> > https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> 


More information about the swift-evolution mailing list