# Resume Pattern Matching in Switch Statements

**URL:** <https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367>\
**Category:** Pitches\
**Tags:** control-flow, switch\
**Created:** [June 7, 2019, 2:28am UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367 "2019-06-07T02:28:06Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![griotspeak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/griotspeak/32/1966_2.png) [@griotspeak](https://forums.swift.org/u/griotspeak)\
**Post date:** [June 11, 2019, 4:11pm UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367/22 "2019-06-11T16:11:57Z")

</div>

Consider it from the directions of case patterns. Currently, repetition of a pattern can, usually, be considered something to warn about ("pattern has already been handled"). If this feature is in, cases that have `resume` are half in half out. They contribute to exhaustivity but repeating them is fine.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [June 11, 2019, 5:30pm UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367/23 "2019-06-11T17:30:05Z")

</div>

Sure, detecting true duplicated patterns will be harder with this feature.

---

<div class="post-metadata">

**Author:** ![Matt\_McLaughlin](https://avatars.discourse-cdn.com/v4/letter/m/ed8c4c/32.png) [@Matt\_McLaughlin](https://forums.swift.org/u/Matt_McLaughlin)\
**Post date:** [June 12, 2019, 6:23pm UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367/24 "2019-06-12T18:23:09Z")

</div>

I agree that this makes it harder to reason about a switch. That said, I rather like the idea of a new `match` function whose syntax is like `switch` but allows for multiple pattern matches (hat tip @Tino). It has the benefits of familiar syntax without muddying the waters for switch itself.

---

<div class="post-metadata">

**Author:** ![fclout](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fclout/32/36155_2.png) [@fclout](https://forums.swift.org/u/fclout)\
**Post date:** [June 12, 2019, 6:29pm UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367/25 "2019-06-12T18:29:03Z")

</div>

Not sure I understand; exhaustivity is about checking that every value gets a case, not that every case gets a value. You can ignore jumps between cases for the purpose of checking exhaustivity.

If we're trying to verify that each case is reachable, then the worst-case scenario would be that you could indirectly jump to cases, and I don't think that anyone wants that. Barring that, unless I'm missing something else, checking that a case is reachable just goes from "a value can match this case" to "a value can match this case OR another reachable case can jump to this case", which, algorithmically, isn't a very challenging new test. The problem of checking that a value can match the case in the first place appears to be much more difficult to me.

---

<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:** [June 12, 2019, 7:13pm UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367/26 "2019-06-12T19:13:13Z")

</div>

The compiler _does_ check that every case is reachable from the original `switch`, in order to catch copy/paste errors. It doesn't do a very exhaustive job of it, but it still checks. "continue matching" would indeed _potentially_ make that logic incorrect, but you'd have to contrive a heck of a switch statement where it'd be necessary (instead of just using `fallthrough`).

---

<div class="post-metadata">

**Author:** ![griotspeak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/griotspeak/32/1966_2.png) [@griotspeak](https://forums.swift.org/u/griotspeak)\
**Post date:** [June 12, 2019, 8:17pm UTC](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367/27 "2019-06-12T20:17:45Z")

</div>

I could be overthinking/overcomplicating the issue since I haven't thought through an entire solution. My roundabout point was that that this would potentially have a complexity cost for implementation and it would only enable more brittle switches. Making the order of cases in a switch matter in a less contiguous way than fall through isn't worth it, in my opinion.,

[Previous page](https://forums.swift.org/t/resume-pattern-matching-in-switch-statements/25367.md?page=1)
