Member variables in method are null when called as delegate from thread

Steven Schveighoffer schveiguy at gmail.com
Tue Jan 12 13:47:05 UTC 2021


On 1/11/21 6:52 PM, Paul Backus wrote:
> On Monday, 11 January 2021 at 16:10:49 UTC, Steven Schveighoffer wrote:
>> There are some... odd rules.
>>
>> struct S
>> {
> [...]
>>    immutable int e = 5; // stored in data segment, not per instance!
> 
> Are you sure?
> 
> struct S
> {
>      immutable int n = 123;
>      this(int n) { this.n = n; }
> }
> 
> void main()
> {
>      S s1;
>      S s2 = 456;
>      assert(s1.n == 123);
>      assert(s2.n == 456);
> }

Yes, I was sure, but clearly wrong ;)

Hm... I guess it was changed! Deprecated in 2.065 [1] and finally 
changed in 2.067 [2]

Now you need static immutable to make it not part of the instance. Which 
makes sense (score 1 for consistency!)

-Steve

[1] https://dlang.org/changelog/2.065.0.html#staticfields2
[2] https://issues.dlang.org/show_bug.cgi?id=3449


More information about the Digitalmars-d-learn mailing list