[swift-evolution] [DRAFT] Regularizing Where Grammar (was Re: Add a while clause to for loops)

Dany St-Amant dsa.mls at icloud.com
Thu Jun 9 20:10:30 CDT 2016


> Le 9 juin 2016 à 15:05, Erica Sadun via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> Gist: https://gist.github.com/erica/86f00c1b8ebf45dcf3507ae6ef642b57
> 
> Regularizing Where grammar
> Proposal: TBD
> Author: Brent Royal-Gordon, Erica Sadun
> Status: TBD
> Review manager: TBD
> Introduction
> 
> This proposal fixes an inconsistency for where clause grammar in Swift language for-in loops.
> 
> Swift Evolution Discussion: Add a while clause to for loops
> 
> Motivation
> 
> Unlike in switch statements and do loops, a for-in loop's where-clause is separated from the pattern it modifies.
> 
> for case? pattern in expression where-clause? code-block
> 
> case-item-list → pattern where-clause? | pattern where-clause? , case-item-list
> 
> catch pattern? where-clause? code-block
> This separation makes the clause harder to associate with the pattern, can confuse users as to whether it modifies the expression or the pattern, and represents an inconsistency in Swift's grammar. This proposal regularizes the grammar to match other uses.
> 
> Note where clauses in case conditions and optional bindings have been removed in SE-0099.
> 

Though, in the 'catch' and 'case' scenarios, as you read the pattern you already know what the pattern is about making the 'where' clause easy to understand. In the case of the 'for' you have no clue of what the pattern is about until you reach the 'in', so it is only once you have read the 'in' expression that you can really understand  impact of the 'where'-clause. So even though the 'for' do not have the same 'pattern where-clause' syntax as the others, the 'where' is probably where it belong for the 'for'

Dany
> Detailed Design
> 
> Current:
> 
> for case? pattern in expression where-clause? code-block
> Proposed:
> 
> for case? pattern where-clause? in expression code-block
> Impact on Existing Code
> 
> Migration should be easily addressed with a simple fix-it.
> 
> Alternatives Considered
> 
> Not accepting this proposal
> 
> 
>>> On Jun 8, 2016, at 9:23 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> This reads to me as “repeat the following block until this fails to be true”, the conditional binding in this case fails to be true if someCondition(value) isn’t true, so the loop ends. I think the key thing here is that the where clause is for the conditional binding and not the loop itself, so in this respect it behaves exactly like an if or guard statement. Meanwhile:
>>> 
>>> 	for eachValue in theValues where someCondition(eachValue) { … }
>>> 
>>> Reads as “for everything in theValues do the following if someCondition(eachValue) is also true”, in other words this loop always tries to visit every element of the sequence (a while loop has no implicit awareness of the sequence, it’s really just an if statement that runs over and over). In this case the where clause is part of the loop itself. There may be an argument that where should be renamed on for loops to better distinguish this, but once you consider that there’s no pattern or conditional binding here I think it makes a reasonable amount of sense.
>> 
>> 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.
>> 
>> -- 
>> Brent Royal-Gordon
>> Architechies
>> 
>> _______________________________________________
>> 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/20160609/7d4fd993/attachment.html>


More information about the swift-evolution mailing list