I think I just figured this out (after getting it wrong several times)…
In the original pitch (the very first post of this topic), the subpattern #unknown
was intended to match only unknown cases. That would have been fine as far as it goes, but here’s the point: There'd still need to be a final catch-all that matched everything that wasn't matched already (which is not the meaning of case #unknown:
, in that definition of #unknown
).
The "match-everything-else" catch-all is needed because otherwise you can't get the diagnostic messages right (for a non-frozen enum).
I think that was the point Jordan got, and has been insisting we get too:
- If the switch was previously (in Swift 4) exhaustive, then adding [hypothetically] just:
case #unknown: // matches only statically unknown cases
…
would, at some future compilation, after more cases had been added to the enum, produce an error that the switch wasn't exhaustive. Most people don't want that to be an error.
- Adding:
case _: // aka 'default:', matches anything else
…
would suppress all warnings about unhandled known cases, now and in the future. Nobody wants no warnings at all.
- Adding [hypothetically] both:
case #unknown: // matches only statically unknown cases
…
case _: // aka 'default:', matches anything else
…
would produce a warning that case _:
cannot be reached, if the switch listed currently-known cases exhaustively. The only way to get rid of the warning would be to remove the one of the cases, leading to one of the other unwanted scenarios.
- Changing the semantics of the
#unknown
subpattern to match "everything unmatched so far" leads to ambiguities between #unknown
and _
in compound match cases (as explained in the most recent posts).
The only correct approach combines those two cases of #3 into a single [hypothetical] case:
case #unknown, _:
which warns if and only the switch fails to enumerate some (presumably “new”) cases known at the time of compilation. This is the desired warning.
Since this catch-call is required regardless of other uses of a [hypothetical] #unknown
subpattern, it may as well have a unique spelling:
unknown default:
which is where the proposal currently stands.
Does that sound right?