Am 19.04.2016 um 12:41 schrieb Jeff Thompson:
I want to create a mutable array of immutable objects, but the code
below gives the error shown below. It seems that "new immutable(C)[1]"
makes the entire array immutable, but it seems I should be able to
change the elements of an array of pointers to an object even though the
objects are immutable. How to do that?

class C {
   this(int x) immutable { this.x = x; }
   int x;
}

void main(string[] args)
{
   auto array = new immutable(C)[1];
   array[0] = new immutable C(10); // Error: Cannot modify immutable
expression array[0].
}


Due to an omission in the type system, this requires the use of the helper type std.typecons.Rebindable:

    auto array = new Rebindable!(immutable(C))[1];
    array[0] = new immutable C(10);

Reply via email to