Associative Array of Const Objects?

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 28 02:45:19 PDT 2015


On Friday, 27 March 2015 at 21:33:19 UTC, bitwise wrote:
> class Test{}
>
> void main()
> {
> 	const(Test)[string] tests;
> 	tests["test"] = new Test();
> }
>
> This code used to work, but after upgrading to dmd 2.067, it no 
> longer does.
> --Error: cannot modify const expression tests["test"]
>
> How do I insert an item into an associative array of const 
> objects?

Generally speaking, you can insert an item in a constructor:
---
class Test{}

const (Test)[string] tests;

static this()
{
     tests["test"] = new Test();
}

class Bar
{
     immutable (Test)[string] tests2;
     this()
     {
         this.tests2["test"] = new Test();
     }
}

void main()
{
     auto bar = new Bar;
}
---

The same problem already existed before 2.067 for AA with strings 
as value (string[string]), since they are immutable.


More information about the Digitalmars-d-learn mailing list