www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19274] New: Inconsistent assignment behavior between struct

https://issues.dlang.org/show_bug.cgi?id=19274

          Issue ID: 19274
           Summary: Inconsistent assignment behavior between struct values
                    and fixed-size arrays
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: stanislav.blinov gmail.com

---
static struct S // static for clarity
{
    bool assigned;
    void opAssign(S) { assigned = true; }
    void opAssign(ref S) { assigned = true; }
}
S value;
assert(!value.assigned);
value = S.init;            // i.e. value = move(otherValue), calls opAssign(S)
assert(value.assigned);    // passes
value.assigned = false;
value = value;             // calls opAssign(ref S)
assert(value.assigned);    // passes

S[1] array;
assert(!array.assigned);
array = array.init;
assert(array[0].assigned); // fails
array[0].assigned = false;
array = array;
assert(array[0].assigned); // fails
---

Snippet above illustrates different behavior for a value and an array. It's a
weird inconsistency, fixed-size array should behave like an aggregate, but it
doesn't.
Justification:

---
static struct Aggregate  // static for clarity
{
    S a;
}
Aggregate aggr; // same as S[1] aggr;
aggr = aggr.init;
assert(aggr.a.assigned);   // passes
---

Same memory, same layout, same aggregated types, different behavior.

--
Sep 28 2018