overiding mutable methods in immutable classes

Eric via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 21 18:37:20 PST 2014


> But think about if enforced immutability is really what you 
> want.
> You don't need to mark the fields immutable to be able to
> construct immutable Xs. A `pure` constructor is handy, as it can
> construct both mutable and immutable objects.
>
> class X
> {
>      private int x;
>      this(int x) pure { this.x = x; }
> }
> void main()
> {
>      auto m = new X(5);
>      auto i = new immutable X(5);
> }
>
>> Note also that (x1 != x2) even though they should be equal (I 
>> think...)
>
> By default, equality of objects is defined as identity. That is,
> an Object is equal only to itself. Override opEquals to change
> that.

I know I can make a class immutable, but the problem is I want
to constrain a template parameter to only immutable types,
and I want to use class types.

-Eric








More information about the Digitalmars-d-learn mailing list