Skip to content

Instantly share code, notes, and snippets.

Last active Jun 9, 2016
What would you like to do?

Regularizing Where grammar


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.

Detailed Design


for case? pattern in expression where-clause? code-block


for case? pattern where-clause? in expression code-block



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 {... }


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 {... }

Impact on Existing Code

Migration should be easily addressed with a simple fix-it.

Alternatives Considered

Not accepting this proposal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment