Skip to content

Instantly share code, notes, and snippets.

@erica
Last active June 9, 2016 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/86f00c1b8ebf45dcf3507ae6ef642b57 to your computer and use it in GitHub Desktop.
Save erica/86f00c1b8ebf45dcf3507ae6ef642b57 to your computer and use it in GitHub Desktop.

Regularizing Where grammar

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.

Detailed Design

Current:

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

Proposed:

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

Examples

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

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