www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to manage the list of immutable objects

reply "aki" <aki google.com> writes:
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.
Jul 14 2015
parent reply "Andrea Fontana" <nospam example.com> writes:
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
Jul 14 2015
parent reply "aki" <aki google.com> writes:
On Tuesday, 14 July 2015 at 10:46:42 UTC, Andrea Fontana wrote:
 Trying on http://dpaste.dzfl.pl/ I don't see that error.
 Maybe you mean "const" and not "immutable" ?

 Andrea
I used: DMD32 D Compiler v2.067.1 on Windows8 I believe there are no typo because it is copy-pasted. I noticed I can use cast. cast()(map[name]) = cast()this; Are there any better solution? Aki.
Jul 14 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/14/15 7:12 AM, aki wrote:
 On Tuesday, 14 July 2015 at 10:46:42 UTC, Andrea Fontana wrote:
 Trying on http://dpaste.dzfl.pl/ I don't see that error.
 Maybe you mean "const" and not "immutable" ?
Wow, that needs updating. Latest version is 2.065 from February 2014, and the git version is from March 2013!
 I used:
 DMD32 D Compiler v2.067.1 on Windows8
 I believe there are no typo because it is copy-pasted.
 I noticed I can use cast.
 cast()(map[name]) = cast()this;
 Are there any better solution?
Cast is not a good idea. What you need is a rebindable class reference to an immutable class. For that, the official recommendation is to use std.typecons.Rebindable. Perhaps it works for you: static Rebindable!(immutable(Foo))[string] map -Steve
Jul 14 2015
parent "aki" <aki google.com> writes:
On Tuesday, 14 July 2015 at 11:44:34 UTC, Steven Schveighoffer 
wrote:
 static Rebindable!(immutable(Foo))[string] map

 -Steve
It all works. And I learned something from the source code of Rebindable. Thank you. Aki.
Jul 14 2015