Properties: a.b.c = 3

Jimbob jim at bob.com
Thu Jul 30 10:35:20 PDT 2009


"Walter Bright" <newshound1 at digitalmars.com> wrote in message 
news:h4ocet$27k$1 at digitalmars.com...
> 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.

Disallow it by default. IE.

a.b.c.d = 3;

If any of those return an rvalue / temporary then it should be an error to 
assign to it.

However we should not disallow...

int i = a.b.c.d.e.f;

But we should also find a way to let the programmer tell the compiler if it 
is ok for it to rewrite...

a.b.c = 3;

as

B tmp = a.b;
tmp.c = 3;
a.b = tmp;

perhaps an extra operator   opSubAssign()   Which tells the compiler it's ok 
to rewrite the the assignment using temporrarys and reassignment.

class A
{
    B _b;
    void b.opAssign(ref B bbb) { _b = bbb; }
    void b.opSubAssign(ref B bbb) { opAssign(bbb); }
}

Then the programmer could also write an specialized subassignment.
 





More information about the Digitalmars-d mailing list