Really nooB question - @property

Kagamin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 21 06:03:23 PDT 2014


On Sunday, 20 July 2014 at 18:14:29 UTC, Eric wrote:
>
>> Use @property when you want a pseudo-variable or something 
>> that might be conceptually considered a "property" of the 
>> object, i.e. to do this:
>>
>> auto blah = thing.someProperty;
>> thing.someProperty = blahblah;
>>
>
> This is basically what I suspected.  But why
> write:
>
> @property int getValue() { return(value); }

You should use nouns to name properties and verbs to name 
methods. D has optional parentheses, so you can write x=getValue;
So @property is generally not needed except for the case when the 
method returns a delegate, which in its turn can be implicitly 
called, so there's an ambiguity, which @property was meant to 
solve (but AFAIK in the end it didn't). It also serves as a 
documentation, that the method should be viewed as a property. 
Initially optional parentheses were meant as an easy 
implementation of properties, and @property wasn't meant to exist 
until the ambiguity with the delegate was understood.


More information about the Digitalmars-d-learn mailing list