Right, I did consider that use case as well. Just to make sure I'm clearly understanding, and for others to follow along as well since it's a bit subtle. If we have an enum like:
enum class Color { Red, Green, Blue };
The proposal considers this exhaustive:
void f(Color color) {
match (color) {
case Color::Red => ;
case Color::Green => ;
case Color::Blue => ;
}
}
Of course this is not really exhaustive since static_cast<Color>(3) is a valid value, but it's not covered here. You could cover it by adding a case _ => ; like:
void f(Color color) {
match (color) {
case Color::Red => ;
case Color::Green => ;
case Color::Blue => ;
case _ => ;
}
}
The problem is that now if we add Yellow as a new color, we don't get any diagnostic about "missing Yellow" since case _ covers it.
So what we want in this situation is to have a fallback case _, but make it such that it doesn't contribute to exhaustiveness checking, such that we still get a diagnostic for "missing Yellow" (This is the behavior we get from -Wswitch-enum).
In the proposal, a pattern that has an if guard does not contribute to exhaustiveness checking. As such, we can leverage this to achieve this by writing:
void f(Color color) {
match (color) {
case Color::Red => ;
case Color::Green => ;
case Color::Blue => ;
case _ if (true) => ;
// ^^^^^^^^^^^^^^^^ catch-all, but don't contribute to exhaustiveness
}
}
An alternative to this weird spelling is to spell it default:
void f(Color color) {
match (color) {
case Color::Red => ;
case Color::Green => ;
case Color::Blue => ;
default => ;
// ^^^^^^^ catch-all, but don't contribute to exhaustiveness
}
}
While I did like the spelling of default better than case _ if (true), I felt that having to explain all of this as a subtle difference between case _ and default was a bit too much. Perhaps my explanation was longer than necessary, but in my experience describing this to folks, it usually takes a while before they're even willing to listen to an explanation as to why case _ and default would be at all different 😅
case _ if (true) is not really intuitive and sounds like a hack more than anything, but perhaps this is a problem with enums and not with patterns. If there was a standard facility to check if enum value is one of the named ones this could maybe become something like case _ if (!std::is_enum_v<Color>(color)) =>
That would technically solve this problem and at least said what it did, but is also wordy as hell and I can't imagine that check function being very performant (basically check for all known values equality, which could be a lot). Also that would be a whole different proposal unrelated to patterns.
I mean... 🤷🏻♂️ haha. What I will say is that case _ if (true) isn't introducing anything new. These parts need to exist independently regardless, so it's "nice" that they fit to serve this purpose. If/when there's a better, specific thing we want to introduce (e.g. default or something else), then that can be added without having to pull out stuff we don't want or something like that.
View all comments
Show parent comment
4
u/mcypark 15h ago edited 15h ago
Right, I did consider that use case as well. Just to make sure I'm clearly understanding, and for others to follow along as well since it's a bit subtle. If we have an
enumlike:The proposal considers this exhaustive:
Of course this is not really exhaustive since
static_cast<Color>(3)is a valid value, but it's not covered here. You could cover it by adding acase _ => ;like:The problem is that now if we add
Yellowas a new color, we don't get any diagnostic about "missingYellow" sincecase _covers it.So what we want in this situation is to have a fallback
case _, but make it such that it doesn't contribute to exhaustiveness checking, such that we still get a diagnostic for "missingYellow" (This is the behavior we get from-Wswitch-enum).In the proposal, a pattern that has an
ifguard does not contribute to exhaustiveness checking. As such, we can leverage this to achieve this by writing:An alternative to this weird spelling is to spell it
default:While I did like the spelling of
defaultbetter thancase _ if (true), I felt that having to explain all of this as a subtle difference betweencase _anddefaultwas a bit too much. Perhaps my explanation was longer than necessary, but in my experience describing this to folks, it usually takes a while before they're even willing to listen to an explanation as to whycase _anddefaultwould be at all different 😅