Properties: a.b.c = 3

Sergey Gromov snake.scaly at gmail.com
Wed Jul 29 10:54:35 PDT 2009


Wed, 29 Jul 2009 13:39:50 +1000, Daniel Keep wrote:

> Walter Bright 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?
>> 
>> It's a classic problem with properties that are implemented as functions.
>> 
>> I don't see how C#'s special property syntax adds any value for dealing
>> with this.
>> 
>> One thought I had was to simply disallow the '.' to appear after a
>> function style property.
> 
> Maybe the compiler could rewrite the above as:
> 
> auto t = a.b;
> t.c = 3;
> a.b = t;
> 
> Unless it can prove it doesn't need to.  Same solution as to the op=
> conundrum.

vote++

Though I'm not sure what to do with this:

void foo(ref int x) {
    x++;
}
foo(a.b.c);

Probably it's not that hard anyway.  Perhaps it's enough to rewrite
property access every time a value-returning property is used as lvalue:

auto t = a.b;
foo(t.c);
a.b = t;



More information about the Digitalmars-d mailing list