Skip to content

Instantly share code, notes, and snippets.

@erica erica/where.md
Last active Jun 9, 2016

Embed
What would you like to do?

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
You can’t perform that action at this time.