www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - array of constants?

reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
I'm trying to define an array of constant like:

===
immutable string[int] MyDict = [
    1: "monday",
    2: "tuesday",
    ];

====
And I keep having compiler error:

Error    1    Error: non-constant expression [1:"monday",2:"tuesday"] 
C:\Dev\DTest\DTest1\Dexperiment\LCIDs.d    9

what can I do? how do I do that? 
Jun 02 2011
parent reply Etherous <etherous gmail.com> writes:
You need to set it in a static constructor
immutable string[int] MyDict;

static this ()
{
  MyDict = cast(string[int]) [
  1: "monday",
  2: "tuesday"
  ];
}
Jun 02 2011
parent "Lloyd Dupont" <ld-REMOVE galador.net> writes:
thanks!

"Etherous"  wrote in message news:is8dbh$n22$1 digitalmars.com... 

You need to set it in a static constructor
immutable string[int] MyDict;

static this ()
{
  MyDict = cast(string[int]) [
  1: "monday",
  2: "tuesday"
  ];
}
Jun 03 2011