[Issue 5207] Immutability is broken in constructors
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Mar 23 22:15:40 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=5207
--- Comment #11 from Nick Treleaven <nick at geany.org> ---
(In reply to hsteoh from comment #7)
> immutable S s;
> immutable S* ptr;
>
> shared static this() {
> ptr = &s;
>
> writeln(ptr.x); // prints 0
>
> s.x = 1;
Interestingly, if it worked like struct/class constructors, you would get an
error:
struct S
{
immutable int x;
immutable int* p;
this(int)
{
p = &x; // compiler counts this as initializing x!
x = 5; // Error: immutable field `x` initialized multiple times
}
}
It also says 'Previous initialization is here.' for the `p = &x` line.
--
More information about the Digitalmars-d-bugs
mailing list