UDP enhancement

JS js.mdnq at gmail.com
Sun Jun 30 18:43:40 PDT 2013


On Monday, 1 July 2013 at 01:35:40 UTC, Jonathan M Davis wrote:
> On Monday, July 01, 2013 03:22:15 JS wrote:
>> struct Foo
>> {
>>      @property int data() { return m_data; } // read property
>>      @property int data(int value) { return m_data = value; } 
>> //
>> write property
>>      private: int m_data;
>> }
>> 
>> It would be nice if properties had an internal variable to use
>> instead of having to declare it explicitly:
>> 
>> 
>> struct Foo
>> {
>>      @property int data() { return data.value; } // read 
>> property
>>      @property int data(int value) { return data.value; } // 
>> write
>> property
>> }
>> 
>> This reduces code complexity. If a property does not use the
>> internal variable(which I signify by .value) then it does not 
>> add
>> any storage. This allows one to easily wrap fields into
>> properties without having to create private fields for each
>> property unless needed.
>
> I believe that the way that this sort of enhancement has 
> typically been
> suggested is to do something like
>
> public @property int value;
>
> which would be lowered to something like
>
> public @property int value() @safe const pure nothrow { return 
> _value; }
> public @property int value(int v) @safe pure nothrow { return 
> _value = v; }
> private int _value;
>
> And I think that that's clearer than your suggestion (it's 
> definitely shorter).
> It also doesn't require the compiler to infer anything about 
> whether you meant
> to have it create a variable or not. It simply tells the 
> compiler what to do
> in a more concise manner.
>
> - Jonathan M Davis

But yet absolutely useless and does nothing over using a field 
directly.

If you are worried about the compiler not being able to infer if 
an internal variable needs to be used or not(which is kinda 
ridiculous because it is very simple to do so(if 
propertyname.value is used then there needs to be an internal 
variable, else not), one can just use a new keyword/attribute 
@propertyval or something...



More information about the Digitalmars-d mailing list