What is the difference between enum and shared immutable?

Max Samukha maxsamukha at gmail.com
Fri Oct 30 12:45:17 UTC 2020


On Thursday, 29 October 2020 at 16:45:51 UTC, Ali Çehreli wrote:

>
> immutable string p;
>
> shared static this() {
>   p = environment["PATH"];  // <-- Run time
> }
>

Immutable static variables being implicitly non-thread-local is a 
design mistake. Thread-local immutable variables have the same 
right to exist as immutable class/struct instance fields.

This should be possible without hacks:

immutable int val; // thread-specific value

static this() {
     val = ...;
}


...because it is logically the same as:

struct V {
     immutable int val;
}

V val;

static this() {
     val = V(...);
}


More information about the Digitalmars-d-learn mailing list