Properties
Miles
_______ at _______.____
Thu Jan 8 18:01:26 PST 2009
Bill Baxter wrote:
> On Fri, Jan 9, 2009 at 9:59 AM, Miles <_______ at _______.____> wrote:
>> Both the getter and the setter should return an rvalue. Properties exist
>> so that they are interchangeable with real member variables.
>
> Partly, but also properties exist to provide encapsulation of private state.
> So you gotta be careful when you say they should always return an lvalue.
Wait! Read again what I said... getters and setters should return an
*rvalue*, not lvalue. Everything else you said was based on the idea
that accessors return lvalues, that wasn't what I said...
There are some very special conditions where the getter MAY return an
lvalue. But nothing else. The setter always returns rvalues.
For example, if prop is a common property, defined as:
public property int width {
get() { return m_width; }
set(Point value) { return m_width = value; }
}
Then the following code:
x.width += 5;
Should expand to something like (hopefully, optimized as far as possible):
{
auto tmp = x._get_width();
tmp += 5;
x._set_width(tmp);
}
On the other hand, if the getter is defined to return an lvalue, like
(tentative syntax):
public property int width {
ref get() { return m_width; }
set(Point value) { return m_width = value; }
}
Then the compiler should be able to optimize the same code further:
x._get_width() += 5;
The same for member function calls if the property type is a structure
or class.
More information about the Digitalmars-d
mailing list