DIP23 draft: Fixing properties redux

Steven Schveighoffer schveiguy at yahoo.com
Sun Feb 3 12:28:58 PST 2013


On Sun, 03 Feb 2013 14:42:17 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 2/3/13 11:34 AM, Johannes Pfau wrote:
>> You have to consider cases though where you have both a setter and a
>> getter returning a 'ref value'.
>>
>> @property ref int value();
>> @property void value(int new);
>
> For now I disallow 0-parameter top level properties. This is a good  
> argument for keeping things that way.

I think Johannes' argument applies to non-global properties.

struct X
{
    private int _val;
    @property ref int value() { return _val;}
    @property void value(int newv) { _val = newv; writeln("in setter!");}
}

So should setting 'value' call the specific setter, or call the getter and  
just write it?

My vote would be to call the setter, since it wouldn't exist if the user  
didn't want to hook that call.

However, it certainly begs the question, why would anyone return ref if  
they want to hook setting?  One could always do this:

void foo(ref int v) {v = 5;}
X x;
foo(x.value);

I think it's still too early to make any assumptions at this point.   
Someone may find a good reason for that, and it's certainly just easier to  
allow code that is already valid than it is to come up with a reason to  
have an error.

-Steve


More information about the Digitalmars-d mailing list