Thread overview
Setting immutable static data inside a struct in a module constructor?
Mar 03
aliak
Mar 03
Alex
Mar 03
aliak
March 03
Is it possible to initialize static immutable members of a struct like you could do for a global immutable one?

immutable string a;
struct Test {
  static immutable string b;
}

shared static this() {
  a = "foo";
  Test.b = "bar";
}

Error: cannot modify immutable expression b
March 03
On Sunday, 3 March 2019 at 20:10:14 UTC, aliak wrote:
> Is it possible to initialize static immutable members of a struct like you could do for a global immutable one?
>
> immutable string a;
> struct Test {
>   static immutable string b;
> }
>
> shared static this() {
>   a = "foo";
>   Test.b = "bar";
> }
>
> Error: cannot modify immutable expression b

Hmm... should work, I think...
But this works in any case:

immutable string a;
struct Test {
  static immutable string b;
  shared static this() {
    b = "bar";
  }
}

shared static this() {
  a = "foo";
}

void main(){
    assert(a == "foo");
    assert(Test.b == "bar");
}
March 03
On Sunday, 3 March 2019 at 20:41:36 UTC, Alex wrote:
> On Sunday, 3 March 2019 at 20:10:14 UTC, aliak wrote:
>> Is it possible to initialize static immutable members of a struct like you could do for a global immutable one?
>>
>> immutable string a;
>> struct Test {
>>   static immutable string b;
>> }
>>
>> shared static this() {
>>   a = "foo";
>>   Test.b = "bar";
>> }
>>
>> Error: cannot modify immutable expression b
>
> Hmm... should work, I think...
> But this works in any case:
>
> immutable string a;
> struct Test {
>   static immutable string b;
>   shared static this() {
>     b = "bar";
>   }
> }

Ah, did not know that. Thank you!