Assigning global and static associative arrays

Mike Parker aldacron at gmail.com
Fri Aug 31 09:31:29 PDT 2012


On 8/31/2012 11:38 PM, ixid wrote:
> Why does this not work:
>
> int[string] dayNumbers =
>          [ "Monday"   : 0, "Tuesday" : 1, "Wednesday" : 2,
>            "Thursday" : 3, "Friday"  : 4, "Saturday"  : 5,
>            "Sunday"   : 6 ];
>
> void main() {
>      //Stuff
> }
>
> With the error 'non-constant expression'? This error also seems to
> prevent me making static associative arrays in functions elegantly.

Non-const variables cannot be initialized at module-scope. If you don't 
need to modify the aa, declaring it immutable should allow the 
initialization to work. Otherwise, you can initialize it using a static 
module constructor.

int[string] dayNumbers;

static this()
{
    dayNumbers = ...;
}

void main() {...}


More information about the Digitalmars-d-learn mailing list