Setting up a final switch from a list
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Aug 2 20:26:54 UTC 2023
On Wednesday, August 2, 2023 12:19:55 PM MDT Cecil Ward via Digitalmars-d-
learn wrote:
> Am I right in thinking that final switch can be used to check
> that all the elements in an enum are handled in cases? Thinking
> that this is a worthwhile safety check, I’d like to convert an
> existing list into an enum for use in a final switch. I have an
> existing list which I use elsewhere,
> enum terminators = [ '%', ':', '?' ];
>
> I pass that to some routine. I am wondering how to safely create
> either an enum with values or have the enum with values declared
> first and then create the list from it. That is, maybe start the
> other way around
> enum terminators_t = { percent = '%', colon = ':', qmark = '?' };
> and then automatically generate the list from that. The idea is
> to avoid duplication in the array list and enum value list, so
> that if I ever add one, I will not be able to update the other to
> match.
>
> Any suggestions? I need some good compile-time stuff. It doesn’t
> matter which direction we go in, from one to the other, but my
> money is on the all-values enum generating the array list.
import std.traits : EnumMembers;
enum Terminator
{
percent = '%',
colon = ':',
qmark = '?'
}
enum terminators = [EnumMembers!Terminator];
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list