On Saturday, 22 November 2014 at 17:40:42 UTC, Eric wrote:
Yes, but this is what I really want:

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

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

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

// test.d(16): Error: template instance X!(Y) does not
// match template declaration X(T : immutable(Object))

I'm not sure what you're getting at. That is, I don't see which
parts of the code you want to be as they are.

For example, this works:

class X(T /* optionally, restrict T to class types:*/ /*: const
Object*/)
{
      private T x;
      this(T x) pure { this.x = x; }
}
/* Maybe add a convenience construction function: */
X!T newX(T)(T x) {return new X!T(x);}

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

void main()
{
      immutable(Y) y = new immutable Y(5);
      X!(immutable Y) x = new X!(immutable Y)(y);
      auto x2 = newX(y); /* same as above, but less wordy */
}

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.

Reply via email to