4 ms·
> The example the author gives, throwing an error on the default section, is a bit contrived. I agree that exhaustiveness checks should be pushed into the compi
by pgwhalen 3y ago
> The example the author gives, throwing an error on the default section, is a bit contrived. I agree that exhaustiveness checks should be pushed into the compiler wherever possible and enums are generally less useful than they could be. That said, there are many, many times where you actually want to the use the default section in a switch statement.
It might be worth rereading. Goetz/Bierman are not saying switch defaults are always useless, they are saying that they are often useless. My own experience as a daily Java programmer suggests that the example is not at all contrived - I'm constantly writing switch defaults that will never run.
> Writing in a strongly typed language where you only have to match and handle a few special states out of many would be absolutely abysmal without a default.
Personally, I would much prefer being explicit about which states the code was written to handle, and have the compiler guide me towards asserting that newly added states are valid.