Constructor qualifiers; bug or expected behavior?

RazvanN razvan.nitu1305 at gmail.com
Mon Apr 2 10:26:32 UTC 2018


Hi all,

Let's say we have this code:

struct B
{
     int a;
     this(int a) immutable
     {
         this.a = 7;
     }

     this(int a)
     {
         this.a = 10;
     }
}

void main()
{
     B a = immutable B(2);
     writeln(a.a);
     a.a = 4;

     immutable B a2 = immutable B(3);
     writeln(a2.a);
     a2.a = 3;        // error : cannot modify
}

Both a and a2 will be constructed using the immutable 
constructor, however a is not immutable (a.a = 4 will compile 
fine). Is this the intended behavior? Shouldn't the compiler warn 
me that I'm trying to create a mutable object using the 
constructor for an immutable object? I couldn't find any 
documentation about this.




More information about the Digitalmars-d-learn mailing list