www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15848] New: out doesn't call opAssign()

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

          Issue ID: 15848
           Summary: out doesn't call opAssign()
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schuetzm gmx.net

import std.stdio;

void foo(out Test x) {
    writeln("x.n = ", x.n);
}

struct Test {
    int n;
    ~this() {
        writeln("~this()");
    }
    int opAssign(int val) {
        writefln("opAssign(%s)", val);
        return n = val + 1;
    }
}

void main() {
    Test t;
    foo(t);
    writeln("done");
}

// output:
x.n = 0
done
~this()

Conclusion:
Upon entering foo(), Test.opAssign() is not called. An argument could be made
that `out` shouldn't construct a new struct, not assign an existing one, but in
that case, it would have to call Test.~this(), which doesn't happen either.

--
Mar 29 2016