overiding mutable methods in immutable classes
Eric via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Nov 22 09:40:40 PST 2014
On Saturday, 22 November 2014 at 17:06:29 UTC, anonymous wrote:
> On Saturday, 22 November 2014 at 15:00:00 UTC, Eric wrote:
>> Yes, but if I don't declare the class T as immutable, I don't
>> think this constraint will work.
>
> You're mistaken. It works just fine.
>
> class X /* not immutable */
> {
> private int x;
> this(int x) pure { this.x = x; }
> }
> template foo(T : immutable Object)
> {
> void foo(T thing) {}
> }
> void main()
> {
> foo(new immutable X(5));
> }
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))
More information about the Digitalmars-d-learn
mailing list