# Draft Proposal: Declare variables in 'case' labels with multiple patterns

**URL:** <https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173>\
**Category:** Discussion\
**Created:** [January 23, 2016, 3:39am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173 "2016-01-23T03:39:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew/32/949_2.png) [@Andrew](https://forums.swift.org/u/Andrew)\
**Post date:** [January 23, 2016, 3:39am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/1 "2016-01-23T03:39:23Z")

</div>

Hi,

I'd like to discuss declaring variables in case labels with multiple  
patterns. I've written a draft proposal, but I'd like to discuss it first  
before formally proposing anything.

> <https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md>

In short:

switch value {  
case let .Case1(x, 2), .Case2(2, x):  
&nbsp;&nbsp;&nbsp;&nbsp;print(x)  
}

The original proposal is here, it may need to adapt after discussion, so  
I'll try to keep the proposal at that link up-to-date.

Thanks,  
Andrew

Declare variables in 'case' labels with multiple patterns

&nbsp;&nbsp;&nbsp;- Proposal: SE-0022  
&nbsp;&nbsp;&nbsp;\<[https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt](https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt);  
&nbsp;&nbsp;&nbsp;- Author(s): Andrew Bennett \<[https://github.com/therealbnut&gt](https://github.com/therealbnut&gt);  
&nbsp;&nbsp;&nbsp;- Status: \*In Discussion\*  
&nbsp;&nbsp;&nbsp;- Review manager: \*Not In Review\*

Introduction

In Swift 2, it is possible to match multiple patterns in cases. However  
cases cannot contain multiple patterns if the case declares variables.

The following code currently produces an error:

enum MyEnum {  
&nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
&nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
}  
switch value {  
case let .Case1(x, 2), .Case2(2, x):  
&nbsp;&nbsp;&nbsp;&nbsp;print(x)  
case .Case1, .Case2:  
&nbsp;&nbsp;&nbsp;&nbsp;break  
}

The error is:

`case` labels with multiple patterns cannot declare variables.

This proposal aims to remove this error when each pattern declares the same  
variables with the same types.

Motivation

This change reduces repeditive code, and therefore reduces mistakes. It's  
consistent with multi-pattern matching when variables aren't defined.  
Proposed solution

Allow case labels with multiple patterns to declare patterns by matching  
variable names in each pattern.

Using the following enum:

enum MyEnum {  
&nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
&nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
}

These cases should be possible:

case let .Case1(x, \_), .Case2(\_, x):  
case let .Case1(y, x), .Case2(x, y):  
case let .Case1(x), .Case2(x):  
case .Case1(let x, \_), .Case2(\_, let x):

Detailed design

Allow case labels with multiple patterns if the case labels match the  
following constraints:

&nbsp;&nbsp;&nbsp;- All patterns declare exactly the same variables.  
&nbsp;&nbsp;&nbsp;- The same variable has the same type in each pattern.

Therefore each pattern is able to produce the same variables for the case  
label.  
Impact on existing code

This should have no impact on existing code, although it should offer many  
opportunities for existing code to be simplified.  
Alternatives considered  
Using a closure or inline function

Code repitition can be reduced with one pattern per 'case' and handling the  
result with an inline function.

func handleCases(value: MyEnum, apply: Int -\> Int) -\> Int {  
&nbsp;&nbsp;&nbsp;&nbsp;func handleX(x: Int) -\> Int {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return apply(x) + 1  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;let out: Int  
&nbsp;&nbsp;&nbsp;&nbsp;switch value {  
&nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
&nbsp;&nbsp;&nbsp;&nbsp;case .Case2(2, let x):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
&nbsp;&nbsp;&nbsp;&nbsp;case .Case1, .Case2:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = -1  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;return out  
}

This syntax is much more verbose, makes control flow more confusing, and  
has the limitations of the what the inline function may capture.

In the above example apply cannot be @noescape because handleX captures it.

Also in the above example if out is captured and assigned by handleX then  
it must be var, not let. This can produce shorter syntax, but is not as  
safe; out may accidentally be assigned more than once, additionally out also  
needs to initialized (which may not be possible or desirable).  
Extending the fallthrough syntax

A similar reduction in code repetition can be achieved if fallthrough  
allowed variables to be mapped onto the next case, for example:

switch test {  
&nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fallthrough .Case2(\_, x)  
&nbsp;&nbsp;&nbsp;&nbsp;case .Case2(3, .let x):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("x: \(x)")  
}

This is not as intuitive, is a hack, and fallthrough should probably be  
discouraged. It is much more flexible, a programmer could adjust the value  
of x before fallthrough. Flexibility increases the chances of programmer  
error, perhaps not as much as code-repitition though.

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@Chris\_Lattner](https://forums.swift.org/u/Chris_Lattner)\
**Post date:** [January 23, 2016, 6:53am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/2 "2016-01-23T06:53:04Z")

</div>

+1.

-Chris

> **···**
>
> > On Jan 22, 2016, at 7:39 PM, Andrew Bennett via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Hi,
> > 
> > I'd like to discuss declaring variables in case labels with multiple patterns. I've written a draft proposal, but I'd like to discuss it first before formally proposing anything.
> > 
> > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [January 23, 2016, 6:09pm UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/3 "2016-01-23T18:09:42Z")

</div>

This would be great. Other pattern matching languages with ML heritage can do this, the only reason Swift couldn't was time.

-Joe

> **···**
>
> > On Jan 22, 2016, at 7:39 PM, Andrew Bennett via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Hi,
> > 
> > I'd like to discuss declaring variables in case labels with multiple patterns. I've written a draft proposal, but I'd like to discuss it first before formally proposing anything.
> > 
> > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)
> > 
> > In short:
> > 
> > switch value {  
> > case let .Case1(x, 2), .Case2(2, x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > }
> > 
> > The original proposal is here, it may need to adapt after discussion, so I'll try to keep the proposal at that link up-to-date.
> > 
> > Thanks,  
> > Andrew
> > 
> > Declare variables in 'case' labels with multiple patterns
> > 
> > Proposal: SE-0022 \<[https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt](https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt);  
> > Author(s): Andrew Bennett \<[https://github.com/therealbnut&gt](https://github.com/therealbnut&gt);  
> > Status: In Discussion  
> > Review manager: Not In Review  
> > Introduction
> > 
> > In Swift 2, it is possible to match multiple patterns in cases. However cases cannot contain multiple patterns if the case declares variables.
> > 
> > The following code currently produces an error:
> > 
> > enum MyEnum {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > }  
> > switch value {  
> > case let .Case1(x, 2), .Case2(2, x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > case .Case1, .Case2:  
> > &nbsp;&nbsp;&nbsp;&nbsp;break  
> > }  
> > The error is:
> > 
> > `case` labels with multiple patterns cannot declare variables.  
> > This proposal aims to remove this error when each pattern declares the same variables with the same types.
> > 
> > Motivation
> > 
> > This change reduces repeditive code, and therefore reduces mistakes. It's consistent with multi-pattern matching when variables aren't defined.
> > 
> > Proposed solution
> > 
> > Allow case labels with multiple patterns to declare patterns by matching variable names in each pattern.
> > 
> > Using the following enum:
> > 
> > enum MyEnum {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > }  
> > These cases should be possible:
> > 
> > case let .Case1(x, \_), .Case2(\_, x):  
> > case let .Case1(y, x), .Case2(x, y):  
> > case let .Case1(x), .Case2(x):  
> > case .Case1(let x, \_), .Case2(\_, let x):  
> > Detailed design
> > 
> > Allow case labels with multiple patterns if the case labels match the following constraints:
> > 
> > All patterns declare exactly the same variables.  
> > The same variable has the same type in each pattern.  
> > Therefore each pattern is able to produce the same variables for the case label.
> > 
> > Impact on existing code
> > 
> > This should have no impact on existing code, although it should offer many opportunities for existing code to be simplified.
> > 
> > Alternatives considered
> > 
> > Using a closure or inline function
> > 
> > Code repitition can be reduced with one pattern per 'case' and handling the result with an inline function.
> > 
> > func handleCases(value: MyEnum, apply: Int -\> Int) -\> Int {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleX(x: Int) -\> Int {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return apply(x) + 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;let out: Int  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(2, let x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1, .Case2:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = -1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;return out  
> > }  
> > This syntax is much more verbose, makes control flow more confusing, and has the limitations of the what the inline function may capture.
> > 
> > In the above example apply cannot be @noescape because handleX captures it.
> > 
> > Also in the above example if out is captured and assigned by handleX then it must be var, not let. This can produce shorter syntax, but is not as safe; out may accidentally be assigned more than once, additionally out also needs to initialized (which may not be possible or desirable).
> > 
> > Extending the fallthrough syntax
> > 
> > A similar reduction in code repetition can be achieved if fallthrough allowed variables to be mapped onto the next case, for example:
> > 
> > switch test {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fallthrough .Case2(\_, x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(3, .let x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("x: \(x)")  
> > }  
> > This is not as intuitive, is a hack, and fallthrough should probably be discouraged. It is much more flexible, a programmer could adjust the value of x before fallthrough. Flexibility increases the chances of programmer error, perhaps not as much as code-repitition though.  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Jed\_Lewison](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@Jed\_Lewison](https://forums.swift.org/u/Jed_Lewison)\
**Post date:** [January 24, 2016, 1:08am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/4 "2016-01-24T01:08:37Z")

</div>

Also +1. I've run into this a couple of times in the past week and the proposal would allow shorter and easier to read code.

> **···**
>
> Sent from my iPhone
> 
> > On Jan 23, 2016, at 10:09 AM, Joe Groff via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > This would be great. Other pattern matching languages with ML heritage can do this, the only reason Swift couldn't was time.
> > 
> > -Joe
> > 
> > > On Jan 22, 2016, at 7:39 PM, Andrew Bennett via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > Hi,
> > > 
> > > I'd like to discuss declaring variables in case labels with multiple patterns. I've written a draft proposal, but I'd like to discuss it first before formally proposing anything.
> > > 
> > > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)
> > > 
> > > In short:
> > > 
> > > switch value {  
> > > case let .Case1(x, 2), .Case2(2, x):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > > }
> > > 
> > > The original proposal is here, it may need to adapt after discussion, so I'll try to keep the proposal at that link up-to-date.
> > > 
> > > Thanks,  
> > > Andrew
> > > 
> > > Declare variables in 'case' labels with multiple patterns  
> > > Proposal: SE-0022  
> > > Author(s): Andrew Bennett  
> > > Status: In Discussion  
> > > Review manager: Not In Review  
> > > Introduction
> > > 
> > > In Swift 2, it is possible to match multiple patterns in cases. However cases cannot contain multiple patterns if the case declares variables.
> > > 
> > > The following code currently produces an error:
> > > 
> > > enum MyEnum {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > > }  
> > > switch value {  
> > > case let .Case1(x, 2), .Case2(2, x):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > > case .Case1, .Case2:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;break  
> > > }  
> > > The error is:
> > > 
> > > `case` labels with multiple patterns cannot declare variables.  
> > > This proposal aims to remove this error when each pattern declares the same variables with the same types.
> > > 
> > > Motivation
> > > 
> > > This change reduces repeditive code, and therefore reduces mistakes. It's consistent with multi-pattern matching when variables aren't defined.
> > > 
> > > Proposed solution
> > > 
> > > Allow case labels with multiple patterns to declare patterns by matching variable names in each pattern.
> > > 
> > > Using the following enum:
> > > 
> > > enum MyEnum {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > > }  
> > > These cases should be possible:
> > > 
> > > case let .Case1(x, \_), .Case2(\_, x):  
> > > case let .Case1(y, x), .Case2(x, y):  
> > > case let .Case1(x), .Case2(x):  
> > > case .Case1(let x, \_), .Case2(\_, let x):  
> > > Detailed design
> > > 
> > > Allow case labels with multiple patterns if the case labels match the following constraints:
> > > 
> > > All patterns declare exactly the same variables.  
> > > The same variable has the same type in each pattern.  
> > > Therefore each pattern is able to produce the same variables for the case label.
> > > 
> > > Impact on existing code
> > > 
> > > This should have no impact on existing code, although it should offer many opportunities for existing code to be simplified.
> > > 
> > > Alternatives considered
> > > 
> > > Using a closure or inline function
> > > 
> > > Code repitition can be reduced with one pattern per 'case' and handling the result with an inline function.
> > > 
> > > func handleCases(value: MyEnum, apply: Int -\> Int) -\> Int {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func handleX(x: Int) -\> Int {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return apply(x) + 1  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;&nbsp;let out: Int  
> > > &nbsp;&nbsp;&nbsp;&nbsp;switch value {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(2, let x):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1, .Case2:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = -1  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;&nbsp;return out  
> > > }  
> > > This syntax is much more verbose, makes control flow more confusing, and has the limitations of the what the inline function may capture.
> > > 
> > > In the above example apply cannot be @noescape because handleX captures it.
> > > 
> > > Also in the above example if out is captured and assigned by handleX then it must be var, not let. This can produce shorter syntax, but is not as safe; out may accidentally be assigned more than once, additionally out also needs to initialized (which may not be possible or desirable).
> > > 
> > > Extending the fallthrough syntax
> > > 
> > > A similar reduction in code repetition can be achieved if fallthrough allowed variables to be mapped onto the next case, for example:
> > > 
> > > switch test {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fallthrough .Case2(\_, x)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(3, .let x):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("x: \(x)")  
> > > }  
> > > This is not as intuitive, is a hack, and fallthrough should probably be discouraged. It is much more flexible, a programmer could adjust the value of x before fallthrough. Flexibility increases the chances of programmer error, perhaps not as much as code-repitition though.  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Andrew](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew/32/949_2.png) [@Andrew](https://forums.swift.org/u/Andrew)\
**Post date:** [January 23, 2016, 7:35am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/5 "2016-01-23T07:35:50Z")

</div>

Thanks Chris :)

I accidentally linked a specific commit, here's a proper link:

> <https://github.com/therealbnut/swift-evolution/blob/therealbnut-case-match/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md>

I also fixed a few typos.

> **···**
>
> On Sat, Jan 23, 2016 at 5:53 PM, Chris Lattner \<clattner@apple.com\> wrote:
> 
> > On Jan 22, 2016, at 7:39 PM, Andrew Bennett via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > Hi,
> > 
> > I'd like to discuss declaring variables in case labels with multiple  
> > patterns. I've written a draft proposal, but I'd like to discuss it first  
> > before formally proposing anything.
> > 
> > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)
> > 
> > +1.
> > 
> > -Chris

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [January 25, 2016, 10:15pm UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/6 "2016-01-25T22:15:34Z")

</div>

+1 in general, but as a nitpick I think this should be

case let .Case1(x, 2), let .Case2(2, x):

because the patterns really are distinct. (In other words, it seems weird to me that "case let .Case1(x), .Case2(x)" is different from "case .Case2(x), let .Case1(x)".)

Jordan

> **···**
>
> > On Jan 22, 2016, at 19:39, Andrew Bennett via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Hi,
> > 
> > I'd like to discuss declaring variables in case labels with multiple patterns. I've written a draft proposal, but I'd like to discuss it first before formally proposing anything.
> > 
> > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)
> > 
> > In short:
> > 
> > switch value {  
> > case let .Case1(x, 2), .Case2(2, x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > }
> > 
> > The original proposal is here, it may need to adapt after discussion, so I'll try to keep the proposal at that link up-to-date.
> > 
> > Thanks,  
> > Andrew
> > 
> > Declare variables in 'case' labels with multiple patterns
> > 
> > Proposal: SE-0022 \<[https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt](https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt);  
> > Author(s): Andrew Bennett \<[https://github.com/therealbnut&gt](https://github.com/therealbnut&gt);  
> > Status: In Discussion  
> > Review manager: Not In Review  
> > Introduction
> > 
> > In Swift 2, it is possible to match multiple patterns in cases. However cases cannot contain multiple patterns if the case declares variables.
> > 
> > The following code currently produces an error:
> > 
> > enum MyEnum {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > }  
> > switch value {  
> > case let .Case1(x, 2), .Case2(2, x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > case .Case1, .Case2:  
> > &nbsp;&nbsp;&nbsp;&nbsp;break  
> > }  
> > The error is:
> > 
> > `case` labels with multiple patterns cannot declare variables.  
> > This proposal aims to remove this error when each pattern declares the same variables with the same types.
> > 
> > Motivation
> > 
> > This change reduces repeditive code, and therefore reduces mistakes. It's consistent with multi-pattern matching when variables aren't defined.
> > 
> > Proposed solution
> > 
> > Allow case labels with multiple patterns to declare patterns by matching variable names in each pattern.
> > 
> > Using the following enum:
> > 
> > enum MyEnum {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > }  
> > These cases should be possible:
> > 
> > case let .Case1(x, \_), .Case2(\_, x):  
> > case let .Case1(y, x), .Case2(x, y):  
> > case let .Case1(x), .Case2(x):  
> > case .Case1(let x, \_), .Case2(\_, let x):  
> > Detailed design
> > 
> > Allow case labels with multiple patterns if the case labels match the following constraints:
> > 
> > All patterns declare exactly the same variables.  
> > The same variable has the same type in each pattern.  
> > Therefore each pattern is able to produce the same variables for the case label.
> > 
> > Impact on existing code
> > 
> > This should have no impact on existing code, although it should offer many opportunities for existing code to be simplified.
> > 
> > Alternatives considered
> > 
> > Using a closure or inline function
> > 
> > Code repitition can be reduced with one pattern per 'case' and handling the result with an inline function.
> > 
> > func handleCases(value: MyEnum, apply: Int -\> Int) -\> Int {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleX(x: Int) -\> Int {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return apply(x) + 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;let out: Int  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(2, let x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1, .Case2:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = -1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;return out  
> > }  
> > This syntax is much more verbose, makes control flow more confusing, and has the limitations of the what the inline function may capture.
> > 
> > In the above example apply cannot be @noescape because handleX captures it.
> > 
> > Also in the above example if out is captured and assigned by handleX then it must be var, not let. This can produce shorter syntax, but is not as safe; out may accidentally be assigned more than once, additionally out also needs to initialized (which may not be possible or desirable).
> > 
> > Extending the fallthrough syntax
> > 
> > A similar reduction in code repetition can be achieved if fallthrough allowed variables to be mapped onto the next case, for example:
> > 
> > switch test {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fallthrough .Case2(\_, x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(3, .let x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("x: \(x)")  
> > }  
> > This is not as intuitive, is a hack, and fallthrough should probably be discouraged. It is much more flexible, a programmer could adjust the value of x before fallthrough. Flexibility increases the chances of programmer error, perhaps not as much as code-repitition though.  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [January 24, 2016, 1:09am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/7 "2016-01-24T01:09:40Z")

</div>

+1 too

-- E

> **···**
>
> > On Jan 23, 2016, at 6:08 PM, Jed Lewison via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Also +1. I've run into this a couple of times in the past week and the proposal would allow shorter and easier to read code.
> > 
> > Sent from my iPhone
> > 
> > On Jan 23, 2016, at 10:09 AM, Joe Groff via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > 
> > > This would be great. Other pattern matching languages with ML heritage can do this, the only reason Swift couldn't was time.
> > > 
> > > -Joe
> > > 
> > > > On Jan 22, 2016, at 7:39 PM, Andrew Bennett via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > 
> > > > Hi,
> > > > 
> > > > I'd like to discuss declaring variables in case labels with multiple patterns. I've written a draft proposal, but I'd like to discuss it first before formally proposing anything.
> > > > 
> > > > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)
> > > > 
> > > > In short:
> > > > 
> > > > switch value {  
> > > > case let .Case1(x, 2), .Case2(2, x):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > > > }
> > > > 
> > > > The original proposal is here, it may need to adapt after discussion, so I'll try to keep the proposal at that link up-to-date.
> > > > 
> > > > Thanks,  
> > > > Andrew
> > > > 
> > > > Declare variables in 'case' labels with multiple patterns
> > > > 
> > > > Proposal: SE-0022 \<[https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt](https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt);  
> > > > Author(s): Andrew Bennett \<[https://github.com/therealbnut&gt](https://github.com/therealbnut&gt);  
> > > > Status: In Discussion  
> > > > Review manager: Not In Review  
> > > > Introduction
> > > > 
> > > > In Swift 2, it is possible to match multiple patterns in cases. However cases cannot contain multiple patterns if the case declares variables.
> > > > 
> > > > The following code currently produces an error:
> > > > 
> > > > enum MyEnum {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > > > }  
> > > > switch value {  
> > > > case let .Case1(x, 2), .Case2(2, x):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > > > case .Case1, .Case2:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;break  
> > > > }  
> > > > The error is:
> > > > 
> > > > `case` labels with multiple patterns cannot declare variables.  
> > > > This proposal aims to remove this error when each pattern declares the same variables with the same types.
> > > > 
> > > > Motivation
> > > > 
> > > > This change reduces repeditive code, and therefore reduces mistakes. It's consistent with multi-pattern matching when variables aren't defined.
> > > > 
> > > > Proposed solution
> > > > 
> > > > Allow case labels with multiple patterns to declare patterns by matching variable names in each pattern.
> > > > 
> > > > Using the following enum:
> > > > 
> > > > enum MyEnum {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > > > }  
> > > > These cases should be possible:
> > > > 
> > > > case let .Case1(x, \_), .Case2(\_, x):  
> > > > case let .Case1(y, x), .Case2(x, y):  
> > > > case let .Case1(x), .Case2(x):  
> > > > case .Case1(let x, \_), .Case2(\_, let x):  
> > > > Detailed design
> > > > 
> > > > Allow case labels with multiple patterns if the case labels match the following constraints:
> > > > 
> > > > All patterns declare exactly the same variables.  
> > > > The same variable has the same type in each pattern.  
> > > > Therefore each pattern is able to produce the same variables for the case label.
> > > > 
> > > > Impact on existing code
> > > > 
> > > > This should have no impact on existing code, although it should offer many opportunities for existing code to be simplified.
> > > > 
> > > > Alternatives considered
> > > > 
> > > > Using a closure or inline function
> > > > 
> > > > Code repitition can be reduced with one pattern per 'case' and handling the result with an inline function.
> > > > 
> > > > func handleCases(value: MyEnum, apply: Int -\> Int) -\> Int {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;func handleX(x: Int) -\> Int {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return apply(x) + 1  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;let out: Int  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;switch value {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(2, let x):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1, .Case2:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = -1  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;return out  
> > > > }  
> > > > This syntax is much more verbose, makes control flow more confusing, and has the limitations of the what the inline function may capture.
> > > > 
> > > > In the above example apply cannot be @noescape because handleX captures it.
> > > > 
> > > > Also in the above example if out is captured and assigned by handleX then it must be var, not let. This can produce shorter syntax, but is not as safe; out may accidentally be assigned more than once, additionally out also needs to initialized (which may not be possible or desirable).
> > > > 
> > > > Extending the fallthrough syntax
> > > > 
> > > > A similar reduction in code repetition can be achieved if fallthrough allowed variables to be mapped onto the next case, for example:
> > > > 
> > > > switch test {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fallthrough .Case2(\_, x)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(3, .let x):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("x: \(x)")  
> > > > }  
> > > > This is not as intuitive, is a hack, and fallthrough should probably be discouraged. It is much more flexible, a programmer could adjust the value of x before fallthrough. Flexibility increases the chances of programmer error, perhaps not as much as code-repitition though.  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Andrew](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andrew/32/949_2.png) [@Andrew](https://forums.swift.org/u/Andrew)\
**Post date:** [January 26, 2016, 1:43am UTC](https://forums.swift.org/t/draft-proposal-declare-variables-in-case-labels-with-multiple-patterns/1173/8 "2016-01-26T01:43:53Z")

</div>

Thanks Jordan,

I wrote a few responses, mostly misinterpreting what you meant, in the end  
I agree with you.

It's more consistent to have a let in front of each case label, so it's of  
the form:  
&nbsp;&nbsp;&nbsp;&nbsp;case \<PATTERN\>, \<PATTERN\>, \<PATTERN\>, ...:

Each pattern is self contained, individually defining whether it's:  
\* let .Case1(1,x), or  
\* .Case1(1, let x)

Yesterday I updated the proposal to state these changes to the existing  
Swift grammar:

+ enum-case-pattern-list → enum-case-pattern |+  
&nbsp;&nbsp;enum-case-pattern , enum-case-pattern-list+ pattern →  
enum-case-pattern-list- pattern → enum-case-pattern

> <https://github.com/therealbnut/swift-evolution/blob/therealbnut-case-match/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md>

It seems that I accidentally already did what you proposed, because the  
grammar was structured like you suggested.

I'll fix the examples in the proposal. It's certainly shorter to only add  
`let` once, but it probably complicates the grammar and pairs the let with  
the case rather than the pattern (which is inconsistent).

The relevant bits of the current grammar:

&nbsp;&nbsp;&nbsp;&nbsp;enum-case-pattern \*→\* type-identifier­ [opt]­ .  
­enum-case-name­ tuple-pattern­ [opt]  
&nbsp;&nbsp;&nbsp;&nbsp;tuple-pattern \*→\* (­tuple-pattern-element-list­ [opt]­)­  
&nbsp;&nbsp;&nbsp;&nbsp;tuple-pattern-element-list \*→\* tuple-pattern-element­ |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tuple-pattern-element­ ,  
­tuple-pattern-element-list  
&nbsp;&nbsp;&nbsp;&nbsp;tuple-pattern-element \*→\* pattern  
&nbsp;&nbsp;&nbsp;&nbsp;pattern \*→\* value-binding-pattern  
&nbsp;&nbsp;&nbsp;&nbsp;value-binding-pattern \*→\* \*var­\* pattern­ |  
\* let\* ­pattern­

> **···**
>
> On Tue, Jan 26, 2016 at 9:15 AM, Jordan Rose \<jordan\_rose@apple.com\> wrote:
> 
> > +1 in general, but as a nitpick I think this should be
> > 
> > case let .Case1(x, 2), let .Case2(2, x):
> > 
> > because the patterns really are distinct. (In other words, it seems weird  
> > to me that "case let .Case1(x), .Case2(x)" is different from "case  
> > .Case2(x), let .Case1(x)".)
> > 
> > Jordan
> > 
> > On Jan 22, 2016, at 19:39, Andrew Bennett via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > Hi,
> > 
> > I'd like to discuss declaring variables in case labels with multiple  
> > patterns. I've written a draft proposal, but I'd like to discuss it first  
> > before formally proposing anything.
> > 
> > [https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md](https://github.com/therealbnut/swift-evolution/blob/a137202e41588b71d3c0511cff85f82ec5f65629/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md)
> > 
> > In short:
> > 
> > switch value {  
> > case let .Case1(x, 2), .Case2(2, x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > }
> > 
> > The original proposal is here, it may need to adapt after discussion, so  
> > I'll try to keep the proposal at that link up-to-date.
> > 
> > Thanks,  
> > Andrew
> > 
> > Declare variables in 'case' labels with multiple patterns
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: SE-0022  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt](https://github.com/therealbnut/swift-evolution/blob/master/proposals/0023-declare-variables-in-case-labels-with-multiple-patterns.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Author(s): Andrew Bennett \<[https://github.com/therealbnut&gt](https://github.com/therealbnut&gt);  
> > &nbsp;&nbsp;&nbsp;- Status: \*In Discussion\*  
> > &nbsp;&nbsp;&nbsp;- Review manager: \*Not In Review\*
> > 
> > Introduction
> > 
> > In Swift 2, it is possible to match multiple patterns in cases. However  
> > cases cannot contain multiple patterns if the case declares variables.
> > 
> > The following code currently produces an error:
> > 
> > enum MyEnum {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > }  
> > switch value {  
> > case let .Case1(x, 2), .Case2(2, x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;print(x)  
> > case .Case1, .Case2:  
> > &nbsp;&nbsp;&nbsp;&nbsp;break  
> > }
> > 
> > The error is:
> > 
> > `case` labels with multiple patterns cannot declare variables.
> > 
> > This proposal aims to remove this error when each pattern declares the  
> > same variables with the same types.
> > 
> > Motivation
> > 
> > This change reduces repeditive code, and therefore reduces mistakes. It's  
> > consistent with multi-pattern matching when variables aren't defined.  
> > Proposed solution
> > 
> > Allow case labels with multiple patterns to declare patterns by matching  
> > variable names in each pattern.
> > 
> > Using the following enum:
> > 
> > enum MyEnum {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case1(Int,Float)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case Case2(Float,Int)  
> > }
> > 
> > These cases should be possible:
> > 
> > case let .Case1(x, \_), .Case2(\_, x):  
> > case let .Case1(y, x), .Case2(x, y):  
> > case let .Case1(x), .Case2(x):  
> > case .Case1(let x, \_), .Case2(\_, let x):
> > 
> > Detailed design
> > 
> > Allow case labels with multiple patterns if the case labels match the  
> > following constraints:
> > 
> > &nbsp;&nbsp;&nbsp;- All patterns declare exactly the same variables.  
> > &nbsp;&nbsp;&nbsp;- The same variable has the same type in each pattern.
> > 
> > Therefore each pattern is able to produce the same variables for the case  
> > label.  
> > Impact on existing code
> > 
> > This should have no impact on existing code, although it should offer many  
> > opportunities for existing code to be simplified.  
> > Alternatives considered  
> > Using a closure or inline function
> > 
> > Code repitition can be reduced with one pattern per 'case' and handling  
> > the result with an inline function.
> > 
> > func handleCases(value: MyEnum, apply: Int -\> Int) -\> Int {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func handleX(x: Int) -\> Int {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return apply(x) + 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;let out: Int  
> > &nbsp;&nbsp;&nbsp;&nbsp;switch value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(2, let x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = handleX(x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1, .Case2:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out = -1  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;return out  
> > }
> > 
> > This syntax is much more verbose, makes control flow more confusing, and  
> > has the limitations of the what the inline function may capture.
> > 
> > In the above example apply cannot be @noescape because handleX captures  
> > it.
> > 
> > Also in the above example if out is captured and assigned by handleX then  
> > it must be var, not let. This can produce shorter syntax, but is not as  
> > safe; out may accidentally be assigned more than once, additionally out also  
> > needs to initialized (which may not be possible or desirable).  
> > Extending the fallthrough syntax
> > 
> > A similar reduction in code repetition can be achieved if fallthrough  
> > allowed variables to be mapped onto the next case, for example:
> > 
> > switch test {  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case1(let x, 2):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fallthrough .Case2(\_, x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;case .Case2(3, .let x):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("x: \(x)")  
> > }
> > 
> > This is not as intuitive, is a hack, and fallthrough should probably be  
> > discouraged. It is much more flexible, a programmer could adjust the value  
> > of x before fallthrough. Flexibility increases the chances of programmer  
> > error, perhaps not as much as code-repitition though.  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
