DIP23 draft: Fixing properties redux

TommiT tommitissari at hotmail.com
Sun Feb 3 10:14:02 PST 2013


On Sunday, 3 February 2013 at 17:46:00 UTC, Andrei Alexandrescu 
wrote:
> On 2/3/13 10:35 AM, TommiT wrote:
>> On Sunday, 3 February 2013 at 08:16:08 UTC, Andrei 
>> Alexandrescu wrote:
>>> Walter and I have had a discussion on how to finalize 
>>> properties.
>>>
>>> http://wiki.dlang.org/DIP23
>>> [..]
>>
>> What happens here?
>>
>> struct S
>> {
>> int _n;
>>
>> @property ref int prop()
>> {
>> return _n;
>> }
>> }
>>
>> @property void prop(ref S s, int n)
>> {
>> s._n = 42;
>> }
>>
>> void main()
>> {
>> S s;
>> s.prop = 10;
>> }
>
> The member is chosen.
>
> Andrei

What happens in these examples?

Example 1:

struct S
{
     int _n;

     @property ref const(int) prop() const
     {
         return _n;
     }
}

@property void prop(ref S s, int n)
{
     s._n = 42;
}

void main()
{
     S s;
     s.prop = 10;
}

--------------------------------------------

Example 2:

struct T
{
     int _n;

     @disable void opAssign(T rhs) { }
}

struct S
{
     T _t;

     @property ref T prop()
     {
         return _t;
     }
}

@property void prop(ref S s, T t)
{
     s._t._n = t._n;
}

void main()
{
     S s;
     s.prop = T.init;
}


More information about the Digitalmars-d mailing list