property functions
Nick
my_dlang at bestmail.us
Mon May 17 15:44:57 UTC 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:
>> 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.
More information about the Digitalmars-d-learn
mailing list