Hi everybody,
I have the following question about "by reference" behavior related to structs.

I have a struct, say S, which contains a member of reference type
struct S
{
    int[] member;
}

and I have a main, for testing:

void main()
{
        S s; // = new S();
        s.arr = [1,2,3];
        writeln(s.arr); //first
        
        S t; // = new S();
        t = s;
        writeln(t.arr); //second
        
        s.arr ~= 4;
        writeln(s.arr); //third
        writeln(t.arr);
}

So, I create a first var of type S, then the second and copied the first into the second. The behavior is like intended, the array inside the struct is copied. But this is a deep copy, as the third block shows. If I change the array in the 's' var, it is not propagated into the 't' var.

What I want to do is: the propagation of changes to all vars, which are copies of the first one.

The idea which works:
change the definition of S into class and add 'new' on defining the vars.
Then, the example shows the desired behavior.

The second idea: implement a postblit into the struct S.
The problem hereby is, how to reference the unique var more then once? How does the postblit looks like? Is it simple a @disable this(this)? But then, I couldn't copy the vars at all... but indeed I don't want to copy them, I rather would like to reference them, if they are intended to mirror the same internal value. Is this idea possible at all?

And last but not least, the question of semantic: how to decide which of the two ways is the right one, if both are possible?

Reply via email to