"switch must be exhaustive" -- but it is

Building on what Joe said, you can address this by adding a default: fatalError() case that the optimizer will strip out later, because it can prove that it's not needed: Compiler Explorer

For the rare case where performance is absolutely critical and there's some invariant that you know about but cannot convince the optimizer to adhere to, you can also do the following: Compiler Explorer. But I really do not recommend this, because unlike the first example, you are introducing undefined behavior if the "invariant" turns out not to be.

19 Likes