What is the state of @property?

zako user at example.net
Mon Aug 29 10:18:31 UTC 2022


On 25.08.22 15:18, Andrey Zherikov wrote:
> 
> I have no objections to deprecate `@property` attribute but I believe D 
> must support the use case when `obj.mem` expression calls `obj.mem()` 
> function (this is usually called property functions).

dmd.50 docs (2002):

--
All this is quite a bit of typing, and it tends to make code unreadable 
by filling it with getProperty() and setProperty() calls. In D, get'ers 
and set'ers take advantage of the idea that an lvalue is a set'er, and 
an rvalue is a get'er:
    class Abc
    {  int myprop;
       void property(int newproperty) { myprop = newproperty; } // set'er
       int property() { return myprop; }   // get'er
    }

which is used as:
    Abc a;
    a.property = 3;      // equivalent to a.property(3)
    int x = a.property;      // equivalent to int x = a.property()

Thus, in D you can treat a property like it was a simple field name. A 
property can start out actually being a simple field name, but if later 
if becomes necessary to make getting and setting it function calls, no 
code needs to be modified other than the class definition.
--



More information about the Digitalmars-d mailing list