@property needed or not needed?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 28 08:21:52 PST 2013


On Mon, 28 Jan 2013 09:00:15 -0500, Maxim Fomin <maxim at maxim-fomin.ru>  
wrote:

> On Monday, 28 January 2013 at 12:31:35 UTC, Jacob Carlborg wrote:
>> On 2013-01-28 12:44, Robert wrote:
>>> Having the compiler lower the following:
>>>
>>> @property int a;
>>>
>>> to
>>>
>>> private int __a;
>>>
>>> @property int a() {
>>> return __a;
>>> }
>>> @property int a(int new_a) {
>>>   __a=new_a;
>>>   return __a;
>>> }

This can be done without compiler help.  But we need @property as a  
primitive to allow it.

>> I would love that. But the setter should return void and the compiler  
>> should to property rewrites.
>
> 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.)?

I think Jacob's point is that a = b = c would lower to:

b = c;
a = b;

But I think it would be wasteful in the given case.  __a is already in the  
register, I think actually the return __a is a noop.

In other cases, where the property value may be a large struct or whatnot,  
not returning the new value from a setter would make sense.

It would be nice if the compiler made the right choice depending on  
whether you returned a value from the property or not.

-Steve


More information about the Digitalmars-d mailing list