Properties: a.b.c = 3

Daniel Keep daniel.keep.lists at gmail.com
Tue Jul 28 22:08:41 PDT 2009



Jarrett Billingsley wrote:
> On Tue, Jul 28, 2009 at 10:33 PM, Walter
> Bright<newshound1 at digitalmars.com> wrote:
>> The issue is what if b is a property, returns a temporary object, and that
>> temp's .c field is uselessly set to 3?
> 
> So, the issue is that 'a.b()' returns a struct by value.  Such return
> values should always be considered rvalues.  Furthermore,
> 'rvalue.field' should also only yield an rvalue.  Therefore, 'a.b.c =
> 3' should be flagged by the compiler as being as nonsensical as "5 =
> x".
> 
> The compiler would flag this as an error, and the solution would of
> course be to make a.b's return value by reference instead.

Problem: what if .b is computed?  Take .length as an example; the setter
MUST be invoked when the length changes, or the stored value won't have
any bearing on the actual number of elements being stored.

Returning refs only works if the property is just pretending to be a
field; in which case the correct solution is to use a field.

Unless it's a property in a class as required by an interface, but you
can't make that ref either because then your interface is dictating
implementation.  :P



More information about the Digitalmars-d mailing list