how to manage the list of immutable objects

aki via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 14 03:09:53 PDT 2015


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.


More information about the Digitalmars-d-learn mailing list