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

monkyyy crazymonkyyy at gmail.com
Tue Jul 29 23:41:55 UTC 2025


On Tuesday, 29 July 2025 at 23:18:18 UTC, Brother Bill wrote:
>
> Your changes do work, but at the cost of making i NOT deeply 
> immutable: no assignment, no changing any elements, no 
> appending, no setting length.

if you insit on the type id suggest this code then

```d
import std.stdio;

immutable int[] i;

shared static this() {
     writeln("In shared static this()");
     int[] i_;
     i_~=43;
     foreach(j;0..10){
         i_~=j;
     }
     i = i_.dup;
}

void main()
{
	writeln("In main()");
	writeln("i: ", i);
}
```

---

this error message is ugly for a newer one; is this a regression? 
Programming in d is sorta spec tier

```d
2.101.2: Failure with output:
-----
/path/to/dmd.linux/dmd2/linux/bin64/../../src/druntime/import/core/internal/array/appending.d(49): Error: cannot modify `immutable` expression `px`
onlineapp.d(6): Error: template instance 
`core.internal.array.appending._d_arrayappendcTXImpl!(immutable(string), immutable(char))` error instantiating
-----

2.102.2 to 2.105.3: Failure with output:
-----
/path/to/dmd.linux/dmd2/linux/bin64/../../src/druntime/import/core/internal/array/appending.d(47): Error: cannot modify `immutable` expression `px`
onlineapp.d(6): Error: template instance 
`core.internal.array.appending._d_arrayappendcTXImpl!(immutable(string), immutable(char))` error instantiating
-----

Since      2.106.1: Failure with output:
-----
/path/to/dmd.linux/dmd2/linux/bin64/../../src/druntime/import/core/internal/array/appending.d(42): Error: cannot modify `immutable` expression `px`
onlineapp.d(6): Error: template instance 
`core.internal.array.appending._d_arrayappendcTX!(immutable(string), immutable(char))` error instantiating
```


More information about the Digitalmars-d-learn mailing list