digitalmars.D.learn - property functions
- Nick (4/8) May 16 2021 Is this warning still valid? The standard library appears to
- Adam D. Ruppe (20/21) May 16 2021 The @property thing doesn't do much. All it does is change
- Paul Backus (10/16) May 16 2021 It's not required:
- Steven Schveighoffer (5/11) May 17 2021 It used to be required, but we removed that requirement a long time ago....
- Adam D. Ruppe (5/7) May 17 2021 yeah i remember ElementType required it last time i checked but
- Nick (10/31) May 17 2021 Thanks for the detailed explanation. I guess the wording of the
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
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
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:It's not required: struct Example { int front() { return 42; } bool empty() { return false; } void popFront() {} } import std.range; static assert(isInputRange!Example);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!)
May 16 2021
On 5/16/21 11:47 AM, Adam D. Ruppe wrote:On Sunday, 16 May 2021 at 15:12:25 UTC, Nick wrote: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). -SteveIs 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!)
May 17 2021
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
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: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.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 17 2021