Omittable parens is an evil

Bruno Medeiros brunodomedeiros+spam at com.gmail
Mon Aug 4 10:16:29 PDT 2008


Jarrett Billingsley wrote:
> "Mike" <vertex at gmx.at> wrote in message news:op.uejk9fs8kgfkbn at lucia...
>> On Sat, 19 Jul 2008 15:32:36 +0200, Koroskin Denis <2korden+dmd at gmail.com> 
>> wrote:
>>
>>> Why not have special syntax for properties, like:
>> This has come up multiple times - that's one of the few things where C# 
>> wins over D. If I may repeat a suggestion I made once, maybe Walter can be 
>> hypnotized into implementing it if I just repeat it often enough :)
>>
>> class foo
>> {
>>     private int _bar;
>>     property int bar
>>     {
>>         opGet() { return _bar; }
>>         opSet(auto value) { _bar = value; }
>>         opAddAssign(auto value) { _bar += value; } // it's extremely 
>> extendable!
>>     }
> 
> I agree, but having to reimplement the opXxxAssign methods for every 
> property would quickly become tedious.  A much simpler way to do it is to 
> define that:
> 
> obj.property op= expr;
> 
> is exactly the same as:
> 
> obj.property.opSet(obj.property.opGet() op expr);
> 
> I believe C# does this.  Still it'd be nice to allow opXxxAssign for 
> properties to overload the default behavior, but it'd at least have a 
> reasonable fallback.
> 

I agree, there is not worth in having to define another accessor other 
than get or set.

My initial brainstorming gives me this design:

class Foo {

   private int bar;

   int op_bar() { return bar; } // getter
   int op_bar(int value) { return bar = value; } // setter

}

No additional "property" syntax is required. You may note that this is 
more redundant (both the "int" type and the "bar" name have duplicate 
occurrences). However, if your properties have default getters and 
setters (the ones that only get and set respectively), then you can just 
use a mixin to avoid the redundancies:


class Foo {

   private int bar; mixin(propGetSet!(bar));

}


> 
> None of this will ever get into D. 
> 
> 

Most likely :(
And it sucks because the implicit function call syntax is more problem 
than worth.

-- 
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list