using enums as key in associative array

Wilfried Kirschenmann wilfried.kirschenmann at gmail.com
Mon Mar 7 07:22:47 PST 2011


Hi,

I'm a beginner in D; I have just begun the book from Andrei.

I would like to know how to statically declare an associative array whose
key are enums.

example :
#!../dmd2/linux/bin/rdmd

import std.stdio, std.string;

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

void main(){
  foreach(deviceSuffix; execDeviceSuffix.values) writeln(deviceSuffix);
}


This won't compile and give the error:
Error: non-constant expression
[cast(deviceType)0:".cpu",cast(deviceType)1:".gpu"]

I have two questions :
1) Why are there casts in the error message ?
Are the enums always substituted by with their base type ?

2) where is the non constant part in this expression ?
Both the deviceType enum and the strings are constant.

Thank you for your help !

-----
Cordialement,
Wilfried Kirschenmann

"An expert is a person who has made all the mistakes that can be made in a
very narrow field."*
*Niels Bohr - *Danish physicist (1885 - 1962)*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110307/c48b3693/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list