www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8006] New: Implement proper in-place-modification for properties

reply d-bugmail puremagic.com writes:
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



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
}


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: -------
Apr 29 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8006


Stewart Gordon <smjg iname.com> changed:

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



*** 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 07 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8006


Jordan Miner <jminer7 gmail.com> changed:

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



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: -------
May 15 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8006


Jacob Carlborg <doob me.com> changed:

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



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: -------
Jan 24 2013