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

ag0aep6g anonymous at example.com
Fri Jul 26 10:53:32 UTC 2019


On 26.07.19 12:40, FeepingCreature wrote:
> On Friday, 26 July 2019 at 10:25:06 UTC, ag0aep6g wrote:
>> 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.
> 
> Sure, in this example you can do that, but in a generic function you 
> have no idea what's inside S.

My point is that you can't do either. You can't mutate immutable data. 
Doesn't matter whether you try it with a `cast` or with `__ctor`. Both 
ways are not allowed.


More information about the Digitalmars-d mailing list