property syntax strawman

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Aug 2 08:56:18 PDT 2009


Denis Koroskin wrote:
> On Sun, 02 Aug 2009 13:00:38 +0400, Michiel Helvensteijn 
> <m.helvensteijn.remove at gmail.com> wrote:
> 
>> Walter Bright wrote:
>>
>>>    bool empty { ... }
>>>    void empty=(bool b) { ... }
>>>
>>> The only problem is when a declaration but not definition is desired:
>>>
>>>    bool empty;
>>>
>>> but oops! That defines a field. So we came up with essentially a hack:
>>>
>>>    bool empty{}
>>>
>>> i.e. the {} means the getter is declared, but defined elsewhere.
>>>
>>> What do you think?
>>
>> It is quite hack-ish. There are ways to have your cake and eat it too. I
>> wouldn't settle for 'bool empty{}'.
>>
>> --------------------------------------------------
>> bool empty {
>>     void set(auto value) { ... }
>>     auto get() { ... }
>> }
>>
>> empty = false; // empty.set(false)
>> auto b = empty; // auto b = empty.get()
>> --------------------------------------------------
>>
>> for example, requires no hacks and no keywords. And has the added 
>> advantage
>> that you can still use the getter and setter methods directly. To call 
>> them
>> or get delegates from them.
>>
> 
> I agree, this is a better solution!

I'd like to have an easy enough syntax for defining read-only properties 
(often in my code). With the proposed syntax, one writes

bool empty { ... }

and calls it a day, but with the elaborate getters and setters there are 
two scopes to get through:

bool empty { auto get() { ... } }

which is quite some aggravation.


Andrei



More information about the Digitalmars-d mailing list