Actual immutability enforcement by placing immutable data into read-only sections

IGotD- nise at nise.com
Mon Dec 19 15:52:29 UTC 2022


On Monday, 19 December 2022 at 15:34:35 UTC, bauss wrote:
>
> Yes, but it's not the reality. Immutable data can be 
> constructed at runtime and it happens all the time in shared 
> static constructors etc. I think it would be a too big breaking 
> change that you suddenly can't do that anymore.
>
> Ex. the following program is valid:
>
> ```
> import std.stdio : writeln;
> import std.datetime : Clock;
>
> immutable int a;
>
> shared static this()
> {
>     a = Clock.currTime().year;
> }
>
> void main()
> {
>     writeln(a);
> }
> ```
>
> In the above example "a" cannot be placed in read-only memory.
>
> Of course my example isn't something you would do in an every 
> day program, BUT it could be substituted for values loaded from 
> a file etc.

Couldn't D could just have used the 'const' keyword for such data.



More information about the Digitalmars-d mailing list