@property for simple methods?

Dennis dkorpel at gmail.com
Mon Apr 2 14:20:49 UTC 2018


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);
	}
}
```


More information about the Digitalmars-d-learn mailing list