ref int value() { return m_value; } - uses?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jul 31 18:36:34 PDT 2011


On Monday 01 August 2011 12:00:02 Joel Christensen wrote:
> > http://www.d-programming-language.org/property.html
> > 
> > @property makes it so that the function is used syntactically as a
> > variable rather than a function. And while it's not currently enforced,
> > eventually, you will _have_ to use property functions with the variable
> > syntax. For instance, empty on ranges is marked with @property:
> > 
> > assert(range.empty);
> > 
> > You could currently do
> > 
> > assert(empty(range));
> > 
> > but eventually that will be illegal. Property functions allow you to
> > have
> > public member variables become functions (or functions become public
> > member variables) without having to change the code which uses them.
> > They're generally used instead of getters and setters.
> > 
> > In this particular case, the property function returns a ref, so it's
> > both a getter and a setter.
> > 
> > - Jonathan M Davis
> 
> Thanks for the reply Davis.
> 
> With the getter/setter method you don't have much control, like knowing
> what value it being set to at the method definition (could with 'ref
> auto xpos( int value ) { ... return m_xpos; }' :-/). And having two
> methods, one for getter and one for setter, you can't do stuff like
> 'xpos++;'

The situation could definitely use some improvement, and there's still more 
ironing out that needs be done with @property implementation-wise. @property 
is supposed to _disallow_ the use parens and the lack of @property is supposed 
to _require_ the use of parens, but we can't enforce that yet due to issues 
with the implementation, and the standard library needs to be adjusted for it 
as well. I believe that the -property flag (or something similar) was added to 
dmd with 2.054 to enable the enforcement of @property, so we can experiment 
with that enforcement now, but we can't always enable it yet.

So, things like not being able to use the increment and decrement operators on 
properties that aren't ref could very well be fixed - I would certainly hope 
so. But it _is_ a bit of an annoyance at the moment.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list