On Tuesday, 14 July 2015 at 10:09:56 UTC, aki wrote:
I like to create immutable object
which is identified by name as it's key.
And also need get() to look up named object
which is already created.

class Foo {
        static immutable(Foo)[string] map;
        string name;
        // and other attributes here...

        this(string name) immutable {
                this.name = name;
                map[name] = this;
        }
        static immutable(Foo) get(string name) {
                return map[name];
        }
}

But this code causes error:
test.d(8): Error: cannot modify immutable expression map[name]

How can I declare the map so that it contains
immutable object Foo, but the reference to Foo is mutable?
Anyway the goal is to provide get() function.
Tell me how to implement get().

Aki.

Trying on http://dpaste.dzfl.pl/ I don't see that error.
Maybe you mean "const" and not "immutable" ?

Andrea

Reply via email to