www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - using enums as key in associative array


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 :

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

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 !

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)*
Mar 07 2011