Associative array literal is non-constant?

Daniel Murphy yebblies at nospamgmail.com
Fri Feb 3 22:43:02 PST 2012


A limitation of the current implementation.  Associative arrays are built on 
the heap, and you can't currently build things on the heap and have them 
exist at runtime.

The best current workaround is probably:
static int[string] table;
static this()
{
  table = ["abc":1, "def":2, "ghi":3];
}

or if inside a function:

static int[string] table;
if (!table)
    table = ["abc":1, "def":2, "ghi":3];



"H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote in message 
news:mailman.356.1328336206.25230.digitalmars-d-learn at puremagic.com...
> Why does the following code give a compiler error?
>
> static int[string] table = ["abc":1, "def":2, "ghi":3];
>
> Error message is:
>
> prog.d:3: Error: non-constant expression ["abc":1,"def":2,"ghi":3]
>
> How is a literal non-constant?
>
>
> T
>
> -- 
> GEEK = Gatherer of Extremely Enlightening Knowledge 




More information about the Digitalmars-d-learn mailing list