DIP 1025--Dynamic Arrays Only Shrink, Never Grow--Community Review Round 1

Adam D. Ruppe destructionator at gmail.com
Mon Nov 11 15:56:02 UTC 2019

On Monday, 11 November 2019 at 15:29:21 UTC, Robert Schadek wrote:
> Now I'm even less sure what this DIP tries to achieve in the 
> long run.

The question isn't the allocation itself, but rather the slice.

void main() {
    int[5] buffer; // or malloc or whatever
    foo(buffer[]); // this is OK

@safe void foo(scope int[] a) {
    a ~= 0; // and this now reallocates on the GC

but i'd argue this is the *right* thing to do. and if you don't 
want the GC function, do @nogc on it, now it is a compile error.

If we were saying that T[] shouldn't have a ~= operator at all... 
well i can kinda see the point, you could use a container object 

But a lot of times the current behavior is perfectly sane. Like 
this example has no memory leak, no out of bounds access, and is 
convenient to use.

If anything maybe we should make @nogc more granular so like have 
independent @nosliceappend or whatever. But like meh.

More information about the Digitalmars-d mailing list