- Proposal: TBD
- Author: Brent Royal-Gordon, Erica Sadun
- Status: TBD
- Review manager: TBD
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
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.
Current:
for case? pattern in expression where-clause? code-block
Proposed:
for case? pattern where-clause? in expression code-block
Before
for eachValue in theValues where eachValue.isOdd {...}
for case .Some(let value) in theValues where value > 5 { ... }
for (eachKey, eachValue) in theKeyValuePairs where eachValue > 5 {... }
After
for eachValue where eachValue.isOdd in theValues { ... }
for case .Some(let value) where value > 5 in theValues { ... }
for (eachKey, eachValue) where eachValue > 5 in theKeyValuePairs {... }
Migration should be easily addressed with a simple fix-it.
Not accepting this proposal