On Tuesday, 25 October 2022 at 14:53:50 UTC, Adam D Ruppe wrote:
On Tuesday, 25 October 2022 at 13:51:30 UTC, Andrey Zherikov wrote:
    A[] a = [A.init];

This is a problem - this is referring to a static array instance, shared across all copies of B. You almost certainly don't want this.

That B.a[0] is the *same object* across different default-constructed Bs... unless the optimizer hits it or something.

Is it a bad idea to trigger copy on write before modification of B.a so it behaves as below?
```d
    B b1;
    b1.a = b1.a.dup;   // copy on write
    b1.a[0].i ~= '1';
    b1.a ~= A.init;
    b1.a[0].i ~= '2';
    b1.a[1].i ~= '3';
    b1.writeln;        // B([A("12"), A("3")])

    B b2;
    b2.writeln;        // B([A("")])
```

Reply via email to