@property for simple methods?

Seb seb at wilzba.ch
Mon Apr 2 15:05:04 UTC 2018


On Monday, 2 April 2018 at 14:51:57 UTC, Vladimirs Nordholm wrote:
> On Monday, 2 April 2018 at 14:20:49 UTC, Dennis wrote:
>> On Monday, 2 April 2018 at 13:57:14 UTC, Vladimirs Nordholm 
>> wrote:
>>> Is there any reason for me to add the @property tags for the 
>>> method?
>>
>> A list of things the @property tag does can be found here:
>> https://dlang.org/spec/function.html#property-functions
>>
>> This behavior is particularly useful for generic code:
>> "For the expression typeof(exp) where exp is an @property 
>> function, the type is the return type of the function, rather 
>> than the type of the function."
>>
>> Before I knew about this, I wrote this template to get the 
>> type of 'field', because typeof(field) would return 'int()' 
>> instead of 'int' when it was a getter function without 
>> @property.
>>
>> ```
>> template ReturnOrValueType(type)
>> {
>> 	static if (isSomeFunction!(type.field)) {
>> 		alias ReturnOrValueType = ReturnType!(typeof(type.field));
>> 	}
>> 	else {
>> 		alias ReturnOrValueType = typeof(type.field);
>> 	}
>> }
>> ```
>
> Ah! First time I read the docs I didn't understand the 
> typeof(exp) explanation, but yours made me understand that part.
>
> Do you think I should I omit the @property tag, if the only 
> wanted behaviour is to set a value (`foo.bar = "baz";`) ?

Yes I would omit @proporty if you don't need it as it isn't 
really useful at the moment.
There's a DIP to fix it and make it more powerful though:

https://github.com/dlang/DIPs/pull/97

And if you are looking for @read, @write limitations the 
accessors library might be interesting to you:

https://code.dlang.org/packages/accessors


More information about the Digitalmars-d-learn mailing list