On Monday, 19 November 2018 at 13:34:50 UTC, Stanislav Blinov wrote:
Because it's not mutation, it's initialization.

Ohhhhh. That's an epiphany for me.

When a ctor is `pure`, the compiler knows it doesn't mutate any state other than the object's, so it allows conversion to all three qualifiers.

I had trouble thinking of an example where impure constructors would break immutable, but now I get it:

```
int* gPtr;

struct S {
  int a;
  this(int a) {
    this.a = a;
    gPtr = &this.a;
  }
}

void main() {
  S s = 1;
  (*gPtr)++;
}
```
s can't be immutable because its field could be mutated, so the constructor needs to be pure (so gPtr can't be set) or immutable (so &this.a is an immutable(int)*).

What you can do, however, if you don't have an const/immutable constructor, is call a mutable constructor explicitly, so long as conversion is possible (i.e. value types)

Interesting.


Reply via email to