Can I check if a value is convertible to a valid value of an enum?

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 24 20:19:31 PDT 2015


On Friday, 25 September 2015 at 03:12:20 UTC, French Football 
wrote:
> ...without having to loop over the enum?
>     writeln( test( valid_value ) ); //prints true

Since `value` is known only at run time, some checks need to be 
performed at run time anyway. One way of doing it without 
iterating over all variants is to create a static hash table

bool[string] valid_strings;

and populate it in static constructor, then in your function you 
can just write

if (value in valid_strings) ...


More information about the Digitalmars-d-learn mailing list