using enums as key in associative array

Wilfried Kirschenmann wilfried.kirschenmann at gmail.com
Tue Mar 8 00:26:10 PST 2011


>> enum deviceType {cpu, gpu}
>> auto execDeviceSuffix = [deviceType.cpu:".cpu", deviceType.gpu:".gpu"];

> The way to get what you want to work in this case is to use a module
> constructor. So, you'd do something like this:
>
> string[deviceType] execDeviceSuffix;
>
> static this()
> {
>        execDeviceSuffix[deviceType.cpu] = "cpu";
>        execDeviceSuffix[deviceType.gpu] = "gpu";
> }
>
> The module constructor will be run before main does, so execDeviceSuffix will be
> properly filled in by then.

I didn't get to the point where module constructors are introduced in
Andrei's book yet. I really like this idea !
I found a similar workaround which used an useless class so that I
could use the static constructor but this is even better !


More information about the Digitalmars-d-learn mailing list