Properties: a.b.c = 3
Zhenyu Zhou
rinick at gmail.com
Wed Jul 29 23:45:35 PDT 2009
Chad J Wrote:
> Zhenyu Zhou wrote:
> >
> > What about:
> >
> > class Widget
> > {
> > Rectangle _rect;
> > immutable(Rectangle) rect() const { return _rect; }
> > Rectangle rect(Rectangle r) { return _rect = r; }
> > }
>
> You'll fall into the trap unless you know about immutable. Said another
> way: the default is unsafe. Also this still isn't very useful, as it
> still requires the calling programmer to manually break apart the
> accesses and will not work for things like "array.length++;" which were
> errors already.
>
> So even if newbies were omniscient and always made their getters
> immutable, all you'd have accomplished is turning a debugging session
> into an annoyance. Noble, but all too bitter when a complete solution
> is within grasp.
Maybe this is what you want
ref rect() { return _rect; }
But I would still return const type because it's unsafe to change the rect without calling its setter
e.g.
Rectangle rect(Rectangle r) {
_rect = r;
redraw();
return _rect;
}
If you allow
widget.rect.w = 200;
widget.rect.h = 100;
you will have to write much more code to handle the painting correctly.
and we don't want to call redraw twice here
More information about the Digitalmars-d
mailing list