www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mutate immutable inside shared static constructor

reply Nick Treleaven <nick geany.org> writes:
I've not used static constructors before, but it seems like the 
following should not be allowed:

```d
import std.stdio;

immutable int x;

 safe shared static this()
{
     x.writeln(); // 0
     x = 5;
     x.writeln(); // 5
     x = 6;
     x++;
     assert(x == 7);
}
```
Should I file a bug to require that `x` is only written to once? 
That would make it consistent with class constructors:

```d
class C
{
     immutable int x;
     this()
     {
         x = 5;
         x = 6; // error, x initialized multiple times
     }
}
```
Mar 23
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Saturday, March 23, 2024 3:23:23 PM MDT Nick Treleaven via Digitalmars-d-
learn wrote:
 I've not used static constructors before, but it seems like the
 following should not be allowed:

 ```d
 import std.stdio;

 immutable int x;

  safe shared static this()
 {
      x.writeln(); // 0
      x = 5;
      x.writeln(); // 5
      x = 6;
      x++;
      assert(x == 7);
 }
 ```
 Should I file a bug to require that `x` is only written to once?
 That would make it consistent with class constructors:

 ```d
 class C
 {
      immutable int x;
      this()
      {
          x = 5;
          x = 6; // error, x initialized multiple times
      }
 }
 ```
Yes, it's a bug. It's a clear violation of the type system if a non-mutable variable is ever given a value more than once. It should be initialized, and then it should be treated as illegal to ever assign to it - or to do anything else which would mutate it. So, clearly, the logic in static constructors with regards to non-mutable variables is overly simple at the moment. - Jonathan M Davis
Mar 23
parent reply Nick Treleaven <nick geany.org> writes:
On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis 
wrote:
 Yes, it's a bug. It's a clear violation of the type system if a 
 non-mutable variable is ever given a value more than once. It 
 should be initialized, and then it should be treated as illegal 
 to ever assign to it - or to do anything else which would 
 mutate it. So, clearly, the logic in static constructors with 
 regards to non-mutable variables is overly simple at the moment.
Thanks, filed: https://issues.dlang.org/show_bug.cgi?id=24449
Mar 23
parent Menjanahary R. R. <megnany afaky.com> writes:
On Saturday, 23 March 2024 at 21:59:57 UTC, Nick Treleaven wrote:
 On Saturday, 23 March 2024 at 21:53:43 UTC, Jonathan M Davis 
 wrote:
 Yes, it's a bug. It's a clear violation of the type system if 
 a non-mutable variable is ever given a value more than once. 
 It should be initialized, and then it should be treated as 
 illegal to ever assign to it - or to do anything else which 
 would mutate it. So, clearly, the logic in static constructors 
 with regards to non-mutable variables is overly simple at the 
 moment.
Thanks, filed: https://issues.dlang.org/show_bug.cgi?id=24449
Thanks for your prompt answer.
Mar 24