Properties sugestion.
serg kovrov
sergk at mailinator.com
Thu Aug 23 13:08:21 PDT 2007
In a nutshell, current implementation of properties is just implicitly
calling function with same name and corresponding parameters.
It's not that bad, just need to be extended.
1. give to programmer means to identify if function could be used as
property. A 'property' keyword seems obvious solution.
2. introduce syntax for append/increment/decrement property.
I believe that's all we really need.
For aesthetic pleasure this 'property' keyword could be used as special
scope to group functions belonging together. But this block should be
optional. Something like:
class A
{
int m_p1;
property int p1() {return m_p;} // getter
property int p1(int i) {return m_p = i;} // setter
real m_p2;
property // full blown property
{
// getter
real p2(real val) {return m_p2;}
// setter
real p2(real val) {m_p2 = val;}
// append/increment/decrement
real p2(bool increment, real val=1.0) {
if (increment) m_p2 += val;
else m_p2 -= val;
}
}
}
Tat way `a.p2++` could be translated to `a.p2(true)`,
`a.p2--` to `a.p2(false)`,
`a.p2 += 2.0` to `a.p2(true, 2.0)`,
etc...
The trick here is to distinguish `a.p++` from `a.p = true`, but I
believe it's solvable.
--
serg.
More information about the Digitalmars-d
mailing list