Qualified destructors / immutable objects

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


no need for ~this() to modify immutable data:

class C {
	int a;

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

struct S {
	C elem = new C(42);
}

void main() {
	import std.stdio;
     immutable(S) s1;

	//  Error: cannot modify immutable expression s1.elem.a
	// s1.elem.a = 43;
	
	writeln(s1.elem.a);

	S s2;
	s2.elem.a = 123;
	writeln(s1.elem.a);
}

Prints:
42
123


More information about the Digitalmars-d-learn mailing list