[Issue 19441] alias this causes partial assignment

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jul 21 15:04:00 UTC 2019


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

--- Comment #16 from johanengelen at weka.io ---
The code of my testcase should now be (with explicit opAssign):
```
EntryType arr;
auto getPtr() { return &arr; } 

struct EntryType {
    int somethingelse;
    bool _state;
    void opAssign(typeof(_state) rhs) {
        _state = rhs;
    }
    alias _state this;
}

struct S1 {
    @property auto ref entry() { return *getPtr(); }
    void opAssign(bool rhs) {
        entry() = rhs;
    }
    alias entry this;
}

void main(){
    S1 s1;
    s1 = true;
}
```

Learnings:
- write to `alias this` only allowed when fully overwriting the struct _itself_
- write to only part of struct: use opAssign
- write to other data than struct itself: use opAssign

--


More information about the Digitalmars-d-bugs mailing list