property += operator

Dlang User dlang.user at gmx.com
Thu May 10 20:03:15 UTC 2018


On 5/10/2018 2:50 PM, SrMordred wrote:
> On Thursday, 10 May 2018 at 19:41:41 UTC, Dlang User wrote:
>> On 5/10/2018 1:43 PM, SrMordred wrote:
>>> [...]
>>
>> I am relatively new to D and I was under the impression that that was 
>> a limitation of @property functions.
>>
>> But, re-reading the language reference, it gave this example (it 
>> returns something from the write property, which seems odd), I 
>> modified to add refs, and then it seems to work, but I am not sure if 
>> it is correct or not:
>>
>> import std.stdio;
>>
>> struct Foo
>> {
>>     @property ref int data() { return m_data; } // read property
>>
>>     @property ref int data(int value) { return m_data = value; } // 
>> write property
>>
>>   private:
>>     int m_data;
>> }
>>
>> void main()
>> {
>>     Foo f;
>>     f.data = 5;
>>     f.data++;
>>     f.data+= 2;
>>     writeln(f.data);
>>
>> }
> 
> this didn´t work either.
> note that 'f.data+= 2;' don't call the write property

That's odd, it works on my machine (Windows 10 with V2.079.0 DMD 
compiler).

I changed main to this:

void main()
{
	Foo f;
	writeln(f.data);
	f.data = 5;
	writeln(f.data);
	f.data++;
	writeln(f.data);
	f.data+= 2;
	writeln(f.data);	
}


and then I get this output:

0
5
6
8



More information about the Digitalmars-d-learn mailing list