[Issue 5207] Immutability is broken in constructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Mar 23 22:07:09 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=5207

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org

--- Comment #10 from Nick Treleaven <nick at geany.org> ---
> isn't the idea of module constructors to not type check qualifiers because it is considered that they are used to initialize data before the program is actually run?

No, because the compiler does check the immutable qualifier for `s` below.

struct S
{
    int* p;
}
immutable S s;

shared static this()
{
    int* p = new int;
    s = S(p); // Error: cannot implicitly convert expression `S(p)` of type `S`
to `immutable(S)`
}

It would also be inconsistent with class constructor immutable field
initialization if immutable was completely ignored in a shared static
constructor - see issue 24449.

--


More information about the Digitalmars-d-bugs mailing list