Properties don't behave like variables?
Jonathan M Davis
jmdavisProg at gmx.com
Mon May 7 14:34:13 PDT 2012
On Monday, May 07, 2012 23:14:36 Jacob Carlborg wrote:
> On 2012-05-07 22:16, Michael wrote:
> > import std.stdio;
> >
> > int pro = 1;
> >
> > @property ref auto prop()
> > {
> >
> > return pro;
> >
> > }
> >
> > @property void prop(int value)
> > {
> >
> > pro = value;
> >
> > }
> >
> > void main()
> > {
> >
> > writeln(prop |= 2);
> >
> > }
>
> You're bypassing the getter.
You mean the setter?
Having a getter property function return by ref does allow you to use a
property exactly as you would a variable, because you're operating on the ref
that's returned. It also makes the property function nigh-on-useless, because
then you're operating on its associated variable outside of the property
function, making it so that you can no longer control access to it. You pretty
much might as well make it a public variable at that point. Not to mention,
even if returning by ref didn't have that problem, it would only work in cases
where the property function was associated with an actual variable (since you
have to return a ref to _something_), so it would still be impossible to
emulate a variable with property functions which calculate the value from
other variables or which grab the value from somewhere else (e.g. a database).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list