# "where" on a switch case with multiple items is misleading

**URL:** <https://forums.swift.org/t/where-on-a-switch-case-with-multiple-items-is-misleading/1761>\
**Category:** Discussion\
**Created:** [March 11, 2016, 12:42pm UTC](https://forums.swift.org/t/where-on-a-switch-case-with-multiple-items-is-misleading/1761 "2016-03-11T12:42:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amir\_Michail](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Amir\_Michail](https://forums.swift.org/u/Amir_Michail)\
**Post date:** [March 11, 2016, 12:42pm UTC](https://forums.swift.org/t/where-on-a-switch-case-with-multiple-items-is-misleading/1761/1 "2016-03-11T12:42:21Z")

</div>

Consider:

switch x {  
&nbsp;&nbsp;case a, b where …: ...  
}

this currently means:

switch x {  
&nbsp;&nbsp;case a where true, b where …: ...  
}

Most people would probably think the “where" applies to both a and b.

To avoid this confusion, maybe if “where" is used for one item then it should be required for all items in that case?

---

<div class="post-metadata">

**Author:** ![Lucas\_Neiva1](https://avatars.discourse-cdn.com/v4/letter/l/7993a0/32.png) [@Lucas\_Neiva1](https://forums.swift.org/u/Lucas_Neiva1)\
**Post date:** [March 11, 2016, 2:51pm UTC](https://forums.swift.org/t/where-on-a-switch-case-with-multiple-items-is-misleading/1761/2 "2016-03-11T14:51:42Z")

</div>

Requiring “where" for all patterns seems like a good idea. I’ve run into this before.

Instinctively I tried to group using ( ), but then you’re matching tuples:

&nbsp;&nbsp;case (a, b) where \<where-expression\>:

Having a way to apply the where-clause to all patterns would be nice. Maybe like this:

&nbsp;&nbsp;case let a, b where all \<where-expression\>:

> **···**
>
> > Consider:
> > 
> > switch x {  
> > case a, b where …: ...  
> > }
> > 
> > this currently means:
> > 
> > switch x {  
> > case a where true, b where …: ...  
> > }
> > 
> > Most people would probably think the “where" applies to both a and b.
> > 
> > To avoid this confusion, maybe if “where" is used for one item then it should be required for all items in that case?

---

<div class="post-metadata">

**Author:** ![SlaunchaMan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slaunchaman/32/520_2.png) [@SlaunchaMan](https://forums.swift.org/u/SlaunchaMan)\
**Post date:** [July 14, 2020, 6:42pm UTC](https://forums.swift.org/t/where-on-a-switch-case-with-multiple-items-is-misleading/1761/3 "2020-07-14T18:42:20Z")

</div>

I just ran into this today. I had no idea that a `where` clause at the end of multiple `case` statements would only apply to the final one, especially when they’re written like this, one `case` on each line followed by a `where` clause:

```swift
switch foo {
case .a,
     .b,
     .c
     where someOtherVariable:
    break
}
```

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [July 14, 2020, 7:44pm UTC](https://forums.swift.org/t/where-on-a-switch-case-with-multiple-items-is-misleading/1761/4 "2020-07-14T19:44:41Z")

</div>

I pitched a potential solution for this problem:

> [@Ability to match a condition to multiple associated values from merged switch cases](https://forums.swift.org/t/ability-to-match-a-condition-to-multiple-associated-values-from-merged-switch-cases/15326):
>
> I just had a small issue with a switch statement where I for some reason assumed that the where clause was already applied to every associated value of a merged switch case. enum Permission { case notSupported(UInt8) case readOnly(UInt8) case readWrite } let permission = Permission.notSupported(0) switch permission { case .notSupported(let code), .readOnly(let code) where code \> 0: /\* create the right error value \*/ default: /\* create the default error value \*/ } The above exa…
