On Fri, Jul 24, 2009 at 6:44 PM, Andrei Alexandrescu<[email protected]> wrote: > Bill Baxter wrote: >> >> On Fri, Jul 24, 2009 at 2:19 PM, Andrei >> Alexandrescu<[email protected]> wrote: >>> >>> 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. >> >> That is true but I also think: >> * probably that is the best that can be achieved automatically. >> * if better can be achieved automatically then the compiler can just >> do that under the hood. No user code needs to change. >> * set(get op value) probably won't be inefficient in some cases. >> * having a op= b work automatically in an inefficient way is better >> than forcing the users of the code to make the same ineffecient >> transformation in their code manually (which is the status quo of >> property syntax in D). >> >> So I think how to make a op= b whizzy fast can be decided later. And >> for now set(get op value) is good enough. > > Ok, I got convinced.
Have you also seen Michiel's suggestion? obj.prop op= value; // auto temp = obj.prop.get(); // temp op= value; // obj.prop.set(value); I think this is probably somewhat more correct, in the face of operator overloading on typeof(temp). That is, if obj.prop is a struct that overloads opAddAssign, you'd want "obj.prop += 5" to really call obj.prop's opAddAssign, rather than its opAdd.
