immutable array in constructor

Jeff Thompson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 17 02:57:37 PDT 2016


In the following code, I explicitly declare array as immutable. 
But it compiles with the error shown below in the comment. The 
array object is declared immutable, so how can the compiler say 
it is a mutable object? In summary, how to pass an immutable 
array to an immutable constructor?

class C {
   int i;
   this(immutable int[] array) immutable {
     i = array[0];
   }
}

void func() {
   immutable int[] array = [1];
   auto c = new C(array); // Error: immutable method C.this is not 
callable using a mutable object
}



More information about the Digitalmars-d-learn mailing list