Re: Immutable objects and constructor ?

2016-05-21 Thread chmike via Digitalmars-d-learn
On Saturday, 21 May 2016 at 08:24:19 UTC, Ali Çehreli wrote: On 05/21/2016 01:07 AM, chmike wrote: > Unfortunately it is not possible to write this > > import std.typecons; > class Info{...} > rebindable!Info x; You have a capitalization typo. Rebindable is a type template, rebindable is a func

Re: Immutable objects and constructor ?

2016-05-21 Thread Ali Çehreli via Digitalmars-d-learn
On 05/21/2016 01:07 AM, chmike wrote: > Unfortunately it is not possible to write this > > import std.typecons; > class Info{...} > rebindable!Info x; You have a capitalization typo. Rebindable is a type template, rebindable is a function template. import std.typecons; class Info{} void main(

Re: Immutable objects and constructor ?

2016-05-21 Thread chmike via Digitalmars-d-learn
Since I'm trying to implement a flyweight pattern, the opEqual need only comparision of reference in my case. By the way, what operation is the switch performing ? OpEqual or is ?

Re: Immutable objects and constructor ?

2016-05-21 Thread chmike via Digitalmars-d-learn
Unfortunately it is not possible to write this import std.typecons; class Info{...} rebindable!Info x; I get the following error message source/app.d(11,3): Error: template std.typecons.rebindable matches more than one template declaration: /usr/include/dmd/phobos/std/typecons.d(1675,14):

Re: Immutable objects and constructor ?

2016-05-20 Thread Mike Parker via Digitalmars-d-learn
On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote: This is confusing and frustrating. In C++ we can write MyInfos { . . . // one is a constant pointer to a constant object of type Obj Obj const * const one; . . . } And in main() Info const * x1 = MyInfos.one; x1 i a modifiable

Re: Immutable objects and constructor ?

2016-05-20 Thread Mike Parker via Digitalmars-d-learn
On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote: But I now met another error in my main(). I can't assign the immutable object to a mutable reference. Info x1 = MyInfos.one; Is it possible to define a mutable reference to an immutable instance ?  This is confusing and frustrating. I

Re: Immutable objects and constructor ?

2016-05-20 Thread Mike Parker via Digitalmars-d-learn
On Friday, 20 May 2016 at 20:30:22 UTC, chmike wrote: I'm a bit surprized that the language doesn't support this. We have immutable strings that can be assigned to different variables. Why couldn't we do the same with objects ? Consider this: immutable(char)[] str; Here, the array element

Re: Immutable objects and constructor ?

2016-05-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, May 20, 2016 20:30:22 chmike via Digitalmars-d-learn wrote: > On Friday, 20 May 2016 at 17:35:01 UTC, Kagamin wrote: > > On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote: > >> But I now met another error in my main(). I can't assign the > >> immutable object to a mutable reference. >

Re: Immutable objects and constructor ?

2016-05-20 Thread chmike via Digitalmars-d-learn
On Friday, 20 May 2016 at 17:35:01 UTC, Kagamin wrote: On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote: But I now met another error in my main(). I can't assign the immutable object to a mutable reference. Info x1 = MyInfos.one; Is it possible to define a mutable reference to an immutabl

Re: Immutable objects and constructor ?

2016-05-20 Thread Kagamin via Digitalmars-d-learn
On Friday, 20 May 2016 at 16:09:54 UTC, chmike wrote: But I now met another error in my main(). I can't assign the immutable object to a mutable reference. Info x1 = MyInfos.one; Is it possible to define a mutable reference to an immutable instance ?  Sort of possible with a library solutio

Re: Immutable objects and constructor ?

2016-05-20 Thread chmike via Digitalmars-d-learn
On Friday, 20 May 2016 at 15:43:28 UTC, Marc Schütz wrote: It looks like your don't actually need `Obj` to be a real nested class. Try declaring it as `static Obj : Info { }`. This should work if `Obj`'s methods don't need access to `MyInfo`'s non-static members. That worked great. Thank y

Re: Immutable objects and constructor ?

2016-05-20 Thread chmike via Digitalmars-d-learn
I solved the problem by moving the class Obj definition out of the class MyInfo. I still don't understand why I had to do that. In C++ this would work without problem. I now have interface Info {. . .} class Obj : Info {. . .} class MyInfos { . . . static immutable Obj one = new immu

Re: Immutable objects and constructor ?

2016-05-20 Thread Marc Schütz via Digitalmars-d-learn
On Friday, 20 May 2016 at 15:07:53 UTC, chmike wrote: The error message is gone, but I now have another compilation error message I don't understand. This is what I have in fact interface Info { . . . } class MyInfos { . . . protected: class Obj : Info { . . . } public:

Re: Immutable objects and constructor ?

2016-05-20 Thread chmike via Digitalmars-d-learn
The error message is gone, but I now have another compilation error message I don't understand. This is what I have in fact interface Info { . . . } class MyInfos { . . . protected: class Obj : Info { . . . } public: static immutable Obj one = new immutable Obj(...);

Re: Immutable objects and constructor ?

2016-05-20 Thread chmike via Digitalmars-d-learn
On Friday, 20 May 2016 at 14:40:23 UTC, Kagamin wrote: On Friday, 20 May 2016 at 14:06:54 UTC, chmike wrote: But when I try to instantiate the class I get an dramatic compilation error: "none of the overloads of '__ctor' are callable using a mutable object, candidates are: " auto a=new immu

Re: Immutable objects and constructor ?

2016-05-20 Thread Kagamin via Digitalmars-d-learn
On Friday, 20 May 2016 at 14:06:54 UTC, chmike wrote: But when I try to instantiate the class I get an dramatic compilation error: "none of the overloads of '__ctor' are callable using a mutable object, candidates are: " auto a=new immutable Info(1,"1"); How should I do if I would like to u

Immutable objects and constructor ?

2016-05-20 Thread chmike via Digitalmars-d-learn
I'm implementing the flyweight pattern. It means that I have a set of object instances representing all the possible values. This allows me to manipulate "values" by simply manipulating references to the instance. Testing "value" equality boils down to simply compare reference value. I hope I