Thread overview
[Issue 8006] New: Implement proper in-place-modification for properties
Apr 30, 2012
Andrej Mitrovic
May 07, 2012
Stewart Gordon
May 16, 2012
Jordan Miner
Jan 25, 2013
Jacob Carlborg
April 30, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8006

           Summary: Implement proper in-place-modification for properties
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrej.mitrovich@gmail.com


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-04-29 23:26:40 PDT ---
Currently properties are only usable for reading and writing values, but they can't really be used for in-place modification:

// fake int type, just to avoid rvalue errors in this demo struct Bar { int x; alias x this; }

struct Foo
{
    Bar _val;
    @property Bar val() { return _val; }
    @property void val(Bar nval) { _val = nval; }
}

void main()
{
    Foo foo;
    foo.val += 5;  // modifies *temporary*, then discards it
    foo.val++;  // ditto
}

The only way to work around this is to make the getter property return by ref, but this completely circumvents the setter property, e.g.:

struct Bar { int x; alias x this; }
struct Foo
{
    Bar _val;
    @property ref Bar val() { return _val; }
    @property void val(Bar nval) { _val = nval; }  // never called
}

void main()
{
    Foo foo;
    foo.val += 5;
    assert(foo.val == 5);  // updated, but setter circumvented
    foo.val++;
    assert(foo.val == 6);  // ditto
}

C# apparently implements in-place modification by translating calls such as
this:
foo.val += 5;
foo.val++;

into this:
foo.val = foo.val + 5;
foo.val = foo.val + 1;

DIP4 also mentioned this feature (http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP4), but was superseeded by DIP6 which was approved. I think we ought to implement this to make properties more usable and less error-prone to work with.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8006


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |wfunction@hotmail.com


--- Comment #1 from Stewart Gordon <smjg@iname.com> 2012-05-07 04:06:21 PDT ---
*** Issue 8056 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8006


Jordan Miner <jminer7@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jminer7@gmail.com


--- Comment #2 from Jordan Miner <jminer7@gmail.com> 2012-05-15 18:40:33 PDT ---
This bug might be a duplicate of bug 808, although @property didn't exist back then. This bug has a more detailed description.

I would really like to see this implemented. Properties seem only half-implemented to me if they don't support +=, -=, etc. operators.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 25, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8006


Jacob Carlborg <doob@me.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob@me.com


--- Comment #3 from Jacob Carlborg <doob@me.com> 2013-01-24 23:42:16 PST ---
Same thing if you do something like this:

foo.val.x = 3;

Needs to be rewritten to:

auto __tmp = foo.val;
__tmp.x = 3;
foo.val = __tmp;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------