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

Joel Christensen joelcnz at gmail.com
Sun Jul 31 17:00:02 PDT 2011


>
> 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++;'

- Joel Christensen


More information about the Digitalmars-d-learn mailing list