Chad J wrote:
Steven Schveighoffer wrote:
struct Rectangle
{
    float x,y,w,h;
}

class Widget
{
    Rectangle _rect;
    Rectangle rect() { return _rect; }
    Rectangle rect(Rectangle r) { return _rect = r; }
    // etc
}

void main()
{
    auto widget = new Widget();

    // DOES WORK:
    auto tmp = widget.rect;
    tmp.w = 200;
    tmp.h = 100;
    widget.rect = tmp;

    // DOES NOT WORK:
    // widget.rect.w = 200;
    // widget.rect.h = 100;
}
Wouldn't the compiler write:

//widget.rect.w = 200 translates to
auto tmp1 = widget.rect;
tmp1.w = 200;
widget.rect = tmp1;

//widget.rect.h = 100 translates to
auto tmp2 = widget.rect;
tmp2.h = 100;
widget.rect = tmp2;

???

Unless you want some serious optimization requirements...

-Steve

It would.

The optimization you speak of is reference caching.  I often do it by
hand in deeply nested loops where it actually means a damn.  It's also
an optimization I think compilers should do, because it is useful in
many more cases than just this.

Using some manner of property syntax would not preclude the programmer
from writing the optimized version of the code by hand.

And, in fact, this exact kind of optimization is made very simple if the compiler uses a static single assignment (SSA) form for its internal code representation. The LLVM suite already does it.

--benji

Reply via email to