struct S { immutable int x; this(int x) { this.x = x; } this(this) { x = 1; } } void main() { S s = S(1); S s2 = s; } Error: can only initialize const member x inside constructor Postblit is a constructor. Why re-initialization is allowed for the state copied from .init and not for the state copied from the source struct?
(In reply to comment #0) > struct S > { > immutable int x; > > this(int x) > { > this.x = x; > } > > this(this) { > x = 1; > } > } > > void main() { > S s = S(1); > S s2 = s; > } > > Error: can only initialize const member x inside constructor > > Postblit is a constructor. Why re-initialization is allowed for the state > copied from .init and not for the state copied from the source struct? This is definitely a compiler bug.
Seems to work with the copy constructor. Postblit is still broken.
Postblit will go the way of the dodo (extinct). Once the deprecation is in master, we can close postblit-related bugs as WONTFIX.
dlang/dmd pull request #11190 "Fix Issue 11292 - Cannot re-initialize a const field in postblit" was merged into master: - 68275da1a9785ad0bf15782da6502948171271bd by Martin Kinkelin: Fix Issue 11292 - Cannot re-initialize a const field in postblit https://github.com/dlang/dmd/pull/11190
dlang/dmd pull request #11921 "Revert "Fix Issue 11292 - Cannot re-initialize a const field in postblit"" was merged into master: - 417649e0e6624b14933387273d13ba0283fe6c08 by Iain Buclaw: Revert "Fix Issue 11292 - Cannot re-initialize a const field in postblit" https://github.com/dlang/dmd/pull/11921
This is an inherent design flaw of the postblit. This is not going to get fixed. Use copy constructor instead.