Bug 808 – using properties as lvalues

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-01-06T20:49:00Z
Last change time
2012-05-31T08:37:19Z
Assigned to
nobody
Creator
dlang-bugzilla

Comments

Comment #0 by dlang-bugzilla — 2007-01-06T20:49:26Z
I've been wondering - what is actually preventing us to operate with properties like real lvalues? For example, int[] array; array.length++; // Error: (array).length is not an lvalue array.length += 2; // Error: (array).length is not an lvalue however, array.length = array.length + 1; // works Unless there's a fair reason of why this isn't viable, I'd suggest implementing this.
Comment #1 by witold.baryluk+d — 2009-04-02T08:49:41Z
Assigment to properties is actually call to some method which actually do other operations (like reallocation). I think that p op= x, properties operations should be defined as p = p op x Also for user defined properties (if both setter and getter is availble, so "lvalue-like" behaviour can be emulated in this way). Cases in which p += x, should be performed differently can always be emulated by using wrapper struct/class, which caries state and overloads opAddAssign. This cases are rear, but still possible. (usefull for some highly optimized codes, or synchronized/atomic changes, or loging actuall changes) array.length *= 2; is one of the most annoying :)
Comment #2 by dsimcha — 2009-05-07T08:06:46Z
*** Bug 2949 has been marked as a duplicate of this bug. ***
Comment #3 by leandro.lucarella — 2012-05-31T04:36:03Z
This is a highly voted enhancement request but there is no "official" statement about it. Would be nice to have some "official" comment for votes with so many votes. Is this intended to be implemented at all? Otherwise I guess is better to close it with WONTFIX to decrese bugzilla's "noise".
Comment #4 by dmitry.olsh — 2012-05-31T04:45:19Z
I'll fork the language if it's not fixed. And of course I'll seduce the whole Russian communitiy to follow suit ;) Seriously, it's important. I've hit it like 10+ time just today.
Comment #5 by dlang-bugzilla — 2012-05-31T04:46:16Z
The examples in my original post, as well as Witold's "array.length *= 2", work now. This issue originally referred to built-in language properties, like .length. The @property kind of properties can be lvalues if the getter returns an lvalue - for example, @property ref int x() { return _x; }, or as Witold mentioned, using wrapper types. I think we can consider this bug as resolved today.
Comment #6 by dmitry.olsh — 2012-05-31T04:47:51Z
Thx Vladimir, I haven't tried ref return. So silly.
Comment #7 by issues.dlang — 2012-05-31T08:37:19Z
And using ref with a property pretty much defeats its entire purpose, since setting it ends up completely bypassing the getter. We really need to be able to have stuff like prop++ work _without_ ref. But there's another enhancement request that's properly specific about that: issue# 8006