On Thursday, 17 March 2016 at 10:04:53 UTC, Anonymouse wrote:
On Thursday, 17 March 2016 at 09:57:37 UTC, Jeff Thompson wrote:
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
}

The error message isn't very good, but remove immutable from the constructor and it works.
  this(immutable int[] array) {

This is a simplified example from a larger class I have where I need an immutable constructor. This is because I need to construct an object an pass it to other functions which take an immutable object. So, how to keep an immutable constructor?

Reply via email to