semi-final switch?

Steven Schveighoffer schveiguy at gmail.com
Thu Jun 17 21:41:28 UTC 2021


A final switch on an enum complains if you don't handle all the enum's 
cases. I like this feature.

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.

Oh, and to throw a monkey wrench in here, the value is a string, not an 
integer. So I can't use std.conv.to to verify the enum is valid (plus, 
then I'm running a switch twice).

Any ideas on better ways to handle this?

-Steve


More information about the Digitalmars-d-learn mailing list