Qualified destructors / immutable objects

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 12 08:00:30 PDT 2015


> I cannot find a way to actually modify immutable memory with 
> it...

a.d:

class C {
   int a;

   this(int a) {
      this.a = a;
   }
}

struct S {
     int x;
     C elem = new C(42);

     ~this() {
         import std.stdio;
         writeln("mutable ~this()");
         x = 1;
         elem.a = 123;
     }
}

void foo() {
     S s2;
}

void main() {
     import std.stdio;
     immutable(S) s1;
     //  Error: cannot modify immutable expression s1.elem.a
     // s1.elem.a = 43;
     writeln(s1.elem.a);
     foo();
     writeln(s1.elem.a);
}

dmd a.d
./a

prints:
42
mutable ~this()
123
mutable ~this()

> Is it a bug?

I'll wait with a bug report, but I think this should be forbidden.


More information about the Digitalmars-d-learn mailing list