how to manage the list of immutable objects

Andrea Fontana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 14 03:46:39 PDT 2015


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


More information about the Digitalmars-d-learn mailing list