2013/12/20 Michel Fortin <[email protected]> > But what if your struct has a class-typed member: > > struct A { > Object o; > int* a; > > this(this) { > a = new int; > o = new Object; > } > > this(this) immutable { > a = new immutable(int); > o = new immutable(Object); // will that work? > } > } > > On the second postblit, the type of "a" has to be "immutable(int)*" to > allow you to assign something else to the pointer while not being able to > affect what's at the other end of the indirection. > > So then, what is the type of "o"? Again, you need to be able to assign the > variable while not affecting what is at the other end of the indirection. > You need a tail-immutable object reference, which doesn't exist.
That's already resolved "design issue" from 2.064, by fixing issue 9665. http://d.puremagic.com/issues/show_bug.cgi?id=9665 Inside constructor, first occured field assignment is automatically handled as the field initializing. struct A { Object o; int* a; this(int) immutable { a = new immutable(int); // is exactly same as: immutable int* a = new immutable(int); o = new immutable(Object); // is exactly same as: immutable(Object) o = new immutable(Object); } Inside postblit, the same rule should be applied. this(this) immutable { a = new immutable(int); // is exactly same as: immutable int* a = new immutable(int); o = new immutable(Object); // is exactly same as: immutable(Object) o = new immutable(Object); } Unfortunately postblit case does not work. I can say it's definitely a compiler bug. http://d.puremagic.com/issues/show_bug.cgi?id=11292 However, I'm purposely delaying to fix the bug, because of DIP49. Kenji Hara
