Associative arrays give compile error

Denis Koroskin 2korden at gmail.com
Tue Oct 5 16:59:46 PDT 2010


On Wed, 06 Oct 2010 03:45:11 +0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

>> /*immutable*/ E_MODE[string] a_mode;
>>
>> static this () {
>>     foreach (m; __traits(allMembers, E_MODE))
>>         mixin(`a_mode["` ~ m ~ `"] = E_MODE.` ~ m ~ `;`);
>> }
>
> How do you build an immutable AA that is global or local to a function?
>
> Bye,
> bearophile

I found the following to work fine:

K[V] assocArray = createAssocArray();

K[V] createAssocArray()
{
     K[V] assocArray = [
        k1: v1,
        k2: v2,
        ...
     ];

     return assocArray;
}

No idea why the following:

K[V] assocArray = [
    k1: v1,
    k2: v2,
    ...
];

doesn't work directly for global scope.


More information about the Digitalmars-d-learn mailing list