Initializing an Immutable Field with Magic: The "Fake Placement New" Technique

ag0aep6g anonymous at example.com
Fri Jul 26 10:25:06 UTC 2019


On 26.07.19 12:11, FeepingCreature wrote:
> How would you initialize an immutable field outside the constructor?

Not, I guess.

[...]
> What a mess. Works though.
> 
> Demo: https://run.dlang.io/is/kg7j3f

That looks like a complicated way of casting away immutable.
`cast(int) value.s.i = 5;` also "works", but has undefined behavior, of 
course. Surely, calling `__ctor` on an existing immutable instance also 
has undefined behavior.


More information about the Digitalmars-d mailing list