digitalmars.D.learn - how to manage the list of immutable objects
- aki (23/23) Jul 14 2015 I like to create immutable object
- Andrea Fontana (4/27) Jul 14 2015 Trying on http://dpaste.dzfl.pl/ I don't see that error.
- aki (8/11) Jul 14 2015 I used:
- Steven Schveighoffer (8/17) Jul 14 2015 Wow, that needs updating. Latest version is 2.065 from February 2014,
- aki (6/8) Jul 14 2015 It all works. And I learned something from
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
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
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" ? AndreaI 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
On 7/14/15 7:12 AM, aki wrote:On Tuesday, 14 July 2015 at 10:46:42 UTC, Andrea Fontana wrote:Wow, that needs updating. Latest version is 2.065 from February 2014, and the git version is from March 2013!Trying on http://dpaste.dzfl.pl/ I don't see that error. Maybe you mean "const" and not "immutable" ?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
On Tuesday, 14 July 2015 at 11:44:34 UTC, Steven Schveighoffer wrote:static Rebindable!(immutable(Foo))[string] map -SteveIt all works. And I learned something from the source code of Rebindable. Thank you. Aki.
Jul 14 2015