semi-final switch?
Steven Schveighoffer
schveiguy at gmail.com
Fri Jun 18 12:22:51 UTC 2021
On 6/18/21 6:35 AM, Johan wrote:
> 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)
Hm... interesting idea! I don't know how to count the number of cases,
but possibly a CTFE validation would be possible (basically use CTFE to
try all the switch cases, and if any throws an exception, then you found
one that's not handled).
-Steve
More information about the Digitalmars-d-learn
mailing list