Wanting an immutable associative array in a class

RedZone kilpa1kb at cmich.edu
Thu Jul 29 17:52:10 PDT 2010


Hello,

I have a class wherein I want an immutable associative array.  I tried to do
it like this:

class Foo
{
private:
     immutable int[char[]] Bar = ["AB":1, "CD":2, "EF":3];

public:
     this()
     {
          ...
     }

     ...
}

But the compiler tells me that ["AB":1, "CD":2, "EF":3] is not constant.

I've gotten around it by doing this, which works:

class Foo
{
private:
     immutable(int)[char[]] Bar;


     void initializeArray()
     {
           immutable(int)[char[]] FooBar = ["AB":1, "CD":2, "EF":3]
           Bar = FooBar;
     }
public:
     this()
     {
           initializeArray();
           ...
     }

     ...
}


But it would be nice if I could have the array reference itself be immutable
and not just the array's contents.  Is there any way I could do this?


More information about the Digitalmars-d-learn mailing list