Programming in D, page 155. shared static this() fails

Luna luna at foxgirls.gay
Tue Jul 29 23:49:31 UTC 2025


On Tuesday, 29 July 2025 at 22:57:50 UTC, Brother Bill wrote:
> ```
> import std.stdio;
>
> immutable int[] i;
>
> shared static this() {
> 	writeln("In shared static this()");
> 	i ~= 43;
> }
>
> void main()
> {
> 	writeln("In main()");
> 	writeln("i: ", i);
> }
> ```
>
> Error messages:
> C:\D\dmd2\windows\bin64\..\..\src\druntime\import\core\internal\array\appending.d(42): Error: cannot modify `immutable` expression `px`
>         px = (cast(T*)pxx.ptr)[0 .. pxx.length];
>         ^
> c:\dev\D\31 - 
> 40\c33_3b_initialization_shared_static_this\source\app.d(7): 
> Error: template instance 
> `core.internal.array.appending._d_arrayappendcTX!(immutable(int[]), immutable(int))` error instantiating
>     i ~= 43;
>       ^
> Failed: ["C:\\D\\dmd2\\windows\\bin64\\dmd.exe", "-v", "-o-", 
> "c:\\dev\\D\\31 - 
> 40\\c33_3b_initialization_shared_static_this\\source\\app.d", 
> "-Ic:\\dev\\D\\31 - 
> 40\\c33_3b_initialization_shared_static_this\\source"]
>
>
> What changes are needed to get this to compile?
> Is this syntax now obsolete?  Is so, what has replaced it?

I think the issue is that you’re appending. An immutable type can 
only be set once by a constructor so you’d want to set it 
directly to an array literal if I remember correctly.


More information about the Digitalmars-d-learn mailing list