semi-final switch?

Johan j at j.nl
Fri Jun 18 10:35:44 UTC 2021


On Thursday, 17 June 2021 at 21:41:28 UTC, Steven Schveighoffer 
wrote:
>
> However, sometimes the data I'm switching on is coming from 
> elsewhere (i.e. a user), and while I want to enforce that the 
> data is valid (it's one of the enum values), I don't want to 
> crash the program if the incoming value is not correct. But 
> final switch doesn't let me declare a default case (to throw an 
> exception instead).
>
> If I use a non-final switch, then my code might forget to 
> handle one of the cases.
>
> Any ideas on better ways to handle this?

Perhaps just a non-final switch, with a static assert comparing 
the number of cases handled to the number of enum members? 
(managable if the number of cases is small and easily countable)

-Johan





More information about the Digitalmars-d-learn mailing list