Hello, everyone!

I have a question regarding the expected behaviour of the built-in array's opSliceAssign.

Given the following code:

```
import std.stdio;

struct A
{
    int x;

    ref A opAssign(A rhs)
    {
        writefln("slice_bug.opAssign: begin");

        return this;
    }
}

void main(string[] args)
{
    A[] a = [A(1), A(2), A(3)];
    A[] b = [A(2), A(3), A(4)];

    // Expecting opAssign to be called for every element in a
    a[] = b[];

    // In other words, I was under the impression that the above
    // is sintactic-sugar for
    for (size_t i = 0; i < a.lenght; ++i)
    {
        a[i] = b[i]; // This calls opAssign, as expected
    }
}
```

As I wrote in the comments above, I was expecting `a[] = b[]` to iterate the slices and assign the elements of b into a.

What really happens is a memcpy: as you can see from godblot [0], this gets lowered to a call to `_d_arraycopy`, in druntime.

I'm not sure if this is the intended behaviour, though.
I'm saying this as I've got bitten by this, because I'm doing reference counting inside opAssign.

IMHO, this is a bug. The code should lower to calls to opAssing for types that define opAssign.

I've also pasted the code on https://run.dlang.io/is/vneELO

[0] - https://godbolt.org/z/_IXCAV


Reply via email to