DIP26: properties defined
Marco Leise
Marco.Leise at gmx.de
Fri Feb 8 21:38:00 PST 2013
Am Fri, 8 Feb 2013 22:13:45 -0500
schrieb Michel Fortin <michel.fortin at michelf.ca>:
> On 2013-02-08 23:53:30 +0000, Robert <jfanatiker at gmx.at> said:
>
> > Ok. Here finally it is:
> >
> > http://wiki.dlang.org/DIP26
>
> This is pretty much exactly how I think properties should work. There's
> two possible benefits you may to have overlooked…
>
> One benefit of the "@property T foo;" syntax that does the boilerplate
> code for you over using a plain variable: the getter and setter
> generated are virtual in a class, so a derived class can override them.
> Doesn't work with a plain variable.
>
> Another possible benefit that we could have is the ability to use the
> default getter but to provide your own setter implementation by just
> declaring it:
>
> @property int a;
> @property void a(int value) { __a = value; refresh(); }
>
> Much cleaner than this:
>
> private int _a;
> @property int a() { return _a; }
> @property void a(int value) { _a = value; refresh(); }
>
> It's really great to not have to write boilerplate functions when
> default behaviour is perfectly fine. I've been using Objective-C for a
> while now and the recent changes where it automatically synthesize a
> variable, a getter, and a setter when declaring a property (unless you
> provide your own) are truly delightful.
There is only little think that itches me. In this example a
is an int. Would the setter always take a copy of a large
struct? I see no way to make it 'auto ref'!
--
Marco
More information about the Digitalmars-d
mailing list