www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - property functions

reply Nick <my_dlang bestmail.us> writes:
The [Property 
Functions](https://dlang.org/spec/function.html#property-functions)
documentation reads:

 WARNING: The definition and usefulness of property functions is 
 being reviewed, and the implementation is currently incomplete. 
 Using property functions is not recommended until the 
 definition is more certain and implementation more mature.
Is this warning still valid? The standard library appears to extensively use property functions.
May 16 2021
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 16 May 2021 at 15:12:25 UTC, Nick wrote:
 Is this warning still valid?
The property thing doesn't do much. All it does is change typeof(a.prop) from function over to the return value of the function. (Which actually makes it required for the range empty and front things!) But since it doesn't do much it makes it very easy to misuse it too - putting it somewhere where it doesn't belong will NOT cause the compiler to issue an error. So that's why it is warned: the current behavior is extremely minimal and if that expands and you misused it, you'll see broken code down the line. But on the other hand, property has been a do-nothing for a decade now, so I wouldn't expect that to change any time soon. My general rule is to put it on something that should be replaceable with a public data member. If you can't do that, don't make it property. In particular, do NOT put it on something for the sole reason of not using () on the call. property is not really related to parenthesis syntax. Only use it when it is specifically meant to be replaceable with a public member.
May 16 2021
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 16 May 2021 at 15:47:55 UTC, Adam D. Ruppe wrote:
 On Sunday, 16 May 2021 at 15:12:25 UTC, Nick wrote:
 Is this warning still valid?
The property thing doesn't do much. All it does is change typeof(a.prop) from function over to the return value of the function. (Which actually makes it required for the range empty and front things!)
It's not required: struct Example { int front() { return 42; } bool empty() { return false; } void popFront() {} } import std.range; static assert(isInputRange!Example);
May 16 2021
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 5/16/21 11:47 AM, Adam D. Ruppe wrote:
 On Sunday, 16 May 2021 at 15:12:25 UTC, Nick wrote:
 Is this warning still valid?
The property thing doesn't do much. All it does is change typeof(a.prop) from function over to the return value of the function. (Which actually makes it required for the range empty and front things!)
It used to be required, but we removed that requirement a long time ago. It was inconsistently applied (some features required property, and some did not). -Steve
May 17 2021
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 17 May 2021 at 14:56:21 UTC, Steven Schveighoffer 
wrote:
 It used to be required, but we removed that requirement a long 
 time ago.
yeah i remember ElementType required it last time i checked but that was a while ago indeed it is all fixed now
May 17 2021
prev sibling parent Nick <my_dlang bestmail.us> writes:
On Sunday, 16 May 2021 at 15:47:55 UTC, Adam D. Ruppe wrote:
 On Sunday, 16 May 2021 at 15:12:25 UTC, Nick wrote:
 Is this warning still valid?
The property thing doesn't do much. All it does is change typeof(a.prop) from function over to the return value of the function. (Which actually makes it required for the range empty and front things!) But since it doesn't do much it makes it very easy to misuse it too - putting it somewhere where it doesn't belong will NOT cause the compiler to issue an error. So that's why it is warned: the current behavior is extremely minimal and if that expands and you misused it, you'll see broken code down the line. But on the other hand, property has been a do-nothing for a decade now, so I wouldn't expect that to change any time soon. My general rule is to put it on something that should be replaceable with a public data member. If you can't do that, don't make it property. In particular, do NOT put it on something for the sole reason of not using () on the call. property is not really related to parenthesis syntax. Only use it when it is specifically meant to be replaceable with a public member.
Thanks for the detailed explanation. I guess the wording of the warning seems strange to me, in that it recommends not to use property functions; but, in actuality (and regardless?), these functions are used a lot (even in the standard library; at least from what I've seen). Also, although this warning is delivered, there is nothing in the documentation that explicitly addresses a concrete downside (or recommended limitation) to using them. However, your explanation does provide some context to this warning.
May 17 2021