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

Brother Bill brotherbill at mail.com
Tue Jul 29 22:57:50 UTC 2025


```
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?



More information about the Digitalmars-d-learn mailing list