overiding mutable methods in immutable classes

Eric via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 22 11:01:42 PST 2014


> But I'm not sure if maybe I changed to much about it.
>
> My point is, that I think it's generally a good idea to be
> flexible when possible, and not make (im)mutability demands
> unless actually necessary.
>
> You may know the following, but I feel like there may be some
> confusion about it: Note that immutable(Y) is a different type
> from just Y. And declaring Y as `immutable class Y` wouldn't
> change a thing about that, as the "immutable" there only affects
> the members of Y. Objects of the bare type Y would still be
> (head) mutable.

I thought I had my code working if instead of declaring
"class Y", I declared "immutable class Y", but now it is
not working for me. But finally this works:

class X(T : immutable Object)
{
     private T x;
     this(T x) pure { this.x = x; }
}

class Y
{
     private const int x;
     this(int x) pure { this.x = x; }
}

void main()
{
     immutable(Y) y = new immutable Y(4);
     X!(immutable(Y)) x = new X!(immutable(Y))(y);
}

Thanks for your help.

-Eric

















More information about the Digitalmars-d-learn mailing list