[Issue 8006] New: Implement proper in-place-modification for properties
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Apr 29 23:25:33 PDT 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 at puremagic.com
ReportedBy: andrej.mitrovich at gmail.com
--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at 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: -------
More information about the Digitalmars-d-bugs
mailing list