On Monday, 29 July 2019 at 05:58:21 UTC, dmm wrote:
So, d try to be smart, only make thing worse?
D is behaving exactly as it should here. You simply have a wrong
model of what an array is in D.
In C++, an array owns its memory. In D, an array is a thin
wrapper around GC managed memory. As such, for instance, you can
take a reference to an array field, then resize the array, and
the original reference will still be valid and at its original
value.
To do what you want, use std.array.Appender, which owns its
memory.
import std.algorithm;
import std.array;
import std.range;
import std.stdio;
void main() {
Appender!(int[]) app;
10.iota.each!(a => app.put(a));
writefln!"%s, %s"(app.data.ptr, app.data);
app.shrinkTo(0);
10.iota.each!(a => app.put(a));
writefln!"%s, %s"(app.data.ptr, app.data);
}