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

Erica Sadun erica at ericasadun.com
Thu Jun 9 10:08:49 CDT 2016


> On Jun 8, 2016, at 9:23 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> The original sin here was in connecting the `where` clause to the for loop's sequence expression, rather than its pattern. If `where` were positioned right after the loop variable:
> 
> 	for eachValue where someCondition(eachValue) in theValues { … }
> 
> It would be much clearer that `where` constrains the values seen by the loop body.
> 
> I'm not sure why the `where` clause was placed where it is. I suspect it has something to do with the `where` clause potentially being more complex than the sequence expression, but I was not in the room where it happened, so that's idle speculation.


I still think `where` should probably be removed from for loops for the reasons
I lay out in the other thread (just use guards, lower confusion, increase readability), 
but you're right. There's something wrong in the way that patterns work outside
switch statements.

Switch statement grammar:
case_item_list : pattern where_clause? | pattern where_clause? ',' case_item_list  
(with inferred initializer at the end)

vs

Case condition grammar:
case_condition : 'case' pattern initializer where_clause?
(with the initializer before the where clause)  

To match, the case_condition should have been:

case_condition : 'case' pattern where_clause? initializer

If SE-0099 is re-evaluated, and I have a strong sense there's a quorum of folk who want 
their `where` clauses, this would introduce more consistent use across the language. 

I plan to stay out of the "oh where oh where can my where clause be" argument. 
I did want to throw in my observations about the inconsistencies between the 
grammars.  Kudos, Brent for spotting that.

-- E

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160609/8ddc68e3/attachment.html>


More information about the swift-evolution mailing list