Jarrett Billingsley wrote:
On Thu, Jul 23, 2009 at 5:56 PM, Michiel
Helvensteijn<[email protected]> wrote:
Eldar Insafutdinov wrote:
from your post:
property int length {
get() { return this.len; }
set(newLen) { this.len = newLen; }
void opIncrement() { this.len++; }
}
I don't think that's flexible to overload every operator to get the best
performance. As Walter likes to say the best way should be the most
obvious. Besides we forgot that D2 allows to return references, which
eliminates the issue.
If your property really just hides a private member variable, it was
probably for encapsulation purposes or because you want redundant actions
to be taken for every access. If you return a reference, you give unlimited
and unrestricted access to that variable with only one call, and you might
as well not have used a property at all.
If your property is derived -- that is, if it doesn't directly mirror a
variable --, there is no reference to return.
Besides, in D, you can probably use mixins to copy the entire interface of a
type into the property without code duplication.
You're suggesting adding something like 25 operator overloads to every
property. Can you say "code bloat"?
Why not just use the following solution, which has been proposed
God-knows-how-many-times and already has precedence in other languages
(like C#)?
obj.prop op= value;
Simply becomes:
obj.prop.set(obj.prop.get op value);
I think this would be inefficient in many cases.
Andrei