www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1974] New: overloaded assignment operators work on non-lvalues


           Summary: overloaded assignment operators work on non-lvalues
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Other
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: default_357-line yahoo.de


module test; import std.stdio;
struct Foo { int y; void opAddAssign(int z) { y += z; } }
struct Bar { Foo fou; Foo test() { return fou; } }
void main() {
  Bar x;
  x.fou.y = 0;
  x.test() += 1337;

The problem is that opAddAssign can be called even though the Foo it's being
called on is not an lvalue at all.

I see two solutions: either make this an error, or try to invoke opIndexAssign
to write the value back after the expression has been evaluated. The first is
probably easier.

Apr 05 2008