@property needed or not needed?

Jacob Carlborg doob at me.com
Tue Nov 20 04:28:25 PST 2012


On 2012-11-19 23:23, Rob T wrote:

> Also do not forget that we can not only drop the (), but also perform
> assignments to functions that take in one variable, but I'm not sure if
> the return must be void for that to work. It seems strange to allow
> arbitrarily dual function/variable constructs for functions, but being
> strange does not necessarily mean it is wrong.

The return value doesn't have to be void. I always return the new value 
from my setters, to allow chained assignments. If chained assignment 
can't be used the property hasn't emulated a variable properly.

class Foo
{
     private int bar_;

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


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

Of course the correct solution would be to implement a form of property 
rewrite in the compiler. Transforming the following code:

int a = foo.bar = 3;

To:

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

If "foo.bar" is a method/@property.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list