using enums as key in associative array

Ali Çehreli acehreli at yahoo.com
Mon Mar 7 11:27:32 PST 2011


On 03/07/2011 11:16 AM, Jonathan M Davis wrote:
 > On Monday, March 07, 2011 07:22:47 Wilfried Kirschenmann wrote:

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

[...]

 >> This won't compile and give the error:
 >> Error: non-constant expression
 >> [cast(deviceType)0:".cpu",cast(deviceType)1:".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";
 > }

This works too:

static this()
{
     execDeviceSuffix = [deviceType.cpu:".cpu", deviceType.gpu:".gpu"];
}

Ali



More information about the Digitalmars-d-learn mailing list