Hi,

I have this program:
----------------------------------------------------
import std.stdio;

void f(ref int[] arr) {
        arr ~= 3;
}

void main() {
        int[][] arrs;
        int[] arr;
        foreach (i; 0 .. 3) {
                arr = new int[0];
                arrs ~= arr; //(a) [[], [], []]
                f(arr);
                // arrs ~= arr; //(b) [[3], [3], [3]]
        }

        writeln(arrs);
}
----------------------------------------------------

This program will print out [[], [], []].

If I comment out (a), and use (b), it will print out [[3], [3], [3]]

So based on this behavior, looks like "~=" will append a copy of `arr`; but what I really want in (a) is append `ref arr` and output [[3], [3], [3]], i.e. the real `arr` be appended instead of its copy.

I have to say this semantics surprised me.

I tried to change arrs' decl to:

    (ref (int[]))[] arrs;  // the intended semantics I want

But I got compiler error out: "found ( when expecting function literal following ref".

1) I'm wondering how to achieve what I want? and
2) why "~=" here will append a copy rather than the real `arr` itself to arrs?


Thanks.

Reply via email to