Qualified destructors / immutable objects

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 14 00:28:37 PDT 2015


To come back to destructors and immutable objects:

Even without the default initialized variables issue it is 
possible to modify immutable data:

struct S {
   int[] bar;

   ~this() {
     bar[0] = 123;
   }
}

void foo(immutable(int[]) i) {
   immutable(S) s = immutable S(i);
}

void main() {
   immutable array = [42, 42];
   foo(array);
   // fails
   assert(array == [42, 42]);
}

I'm not sure whether ~this() should be forbidden to modify 
immutable data. Consider e.g. some fields need to be free'd. If 
someone else is using references to such a field after ~this() -> 
segfault, but it could be seen as analogon to the list of 
undefined operations on pointer to GC memory.
Additionally the same happens already for stack allocated fields 
like int[4] inside a (mutable/const/immutable) struct.



More information about the Digitalmars-d-learn mailing list