Resume Pattern Matching in Switch Statements

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.

1 Like

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

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.

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.

1 Like

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).

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.,