@property needed or not needed?

Jacob Carlborg doob at me.com
Mon Jan 28 11:55:47 PST 2013


On 2013-01-28 15:00, Maxim Fomin wrote:

> Returning void instead of int in the example break assignment chaining a
> = b = c. Besides, how such implicitly defined functions may call user
> defined code (check input validity, call events, etc.)?

No, the compiler should do a rewrite, as follows:

class Foo
{
     int bar_;

     @property int bar () { return bar_; }
     @property void bar (int value) { bar_ = value; }
}

auto foo = new Foo;
int a = foo.bar = 3;

The above line should be rewritten as:

foo.bar = 3;
int a = foo.bar;

The compiler also need to rewrite the following:

struct Bar
{
     int a;
}

class Foo
{
     Bar bar_;

     @property Bar bar () { return bar_; }
     @property void bar (Bar value) { bar_ = value; }
}

auto foo = new Foo;
foo.bar.a = 3;

The above line should be rewritten to:

auto __tmp = foo.bar;
__tmp.a = 3;
foo.bar = __tmp;

If not, the value of "foo.bar.a" hasn't really changed since you 
returned a copy by value. If you instead return by reference you can 
bypass the setter using the getter.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list