@property needed or not needed?

Maxim Fomin maxim at maxim-fomin.ru
Tue Jan 29 07:15:33 PST 2013


On Monday, 28 January 2013 at 19:55:48 UTC, Jacob Carlborg wrote:
> 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;

No, it should be rewritten as foo.bar = 3, int a = 3 or int a = 
foo.bar._set(3). Your example calls getter, which breaks C and D 
assignment rules, and contradicts to C# property implementation 
as was said in this thread in previous posts.

> 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.

This is reasonable.


More information about the Digitalmars-d mailing list