March 07, 2011 using enums as key in associative array | ||||
---|---|---|---|---|
| ||||
Attachments:
| 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)* |
Copyright © 1999-2021 by the D Language Foundation