@property

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 8 14:09:14 PST 2016


On Thursday, December 08, 2016 16:54:57 Adam D. Ruppe via Digitalmars-d-
learn wrote:
> On Thursday, 8 December 2016 at 16:53:13 UTC, Satoshi wrote:
> > is there any advantage of marking function as @property??
>
> Not really. I think it just changes the meaning of
> typeof(thatfunc) but otherwise it does nothing.
>
> However, if you use it in a base class, you must also use it when
> overriding the function.

Yeah, it's pretty common for folks to slap @property on functions to make it
clear that it's intended to be used as a property and thus is a getter or
setter. So, it serves a purpose from the standpoint of documentation. But at
this point, it doesn't make much differently on the technical level. The
differences with typeof and stuff like std.traits.FunctionAttributes are
what would be different. You can use the property syntax whether @property
was used or not.

The history of @property is a bit of a mess. It was an attempt to move away
from the ad-hoc situation with anything being treated as a property if it
had a signature that worked with one (e.g. something like writeln = 7; is
legal), but for various reasons, it never worked out, and we got a partial
transition with @property ultimately being mostly for documentation
purposes.

One place where this is truly a technical problem rather than a stylistic
one is property functions that return callables, since if you try and call
them with parens, you end up just calling the property function (whether
it's marked with @property or not), and you need a second set of parens to
actually call the callable, meaning that it doesn't actually work as a
property. So, @property _might_ be changed at some point in the future to
fix that problem, but given how long it's been the way that it is, there's a
good chance that we're just stuck with how it is, and it's going to do
pretty much nothing on a technical level except cause some corner case
weirdness with typeof and be detectable by introspection. It does serve as
documentation though, which I think is a lot of why many folks who know full
well what the whole situation with properties is continue to use it.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list