Fixing D's Properties

Ender KaShae astrothayne at gmail.com
Sat Aug 18 19:21:32 PDT 2007


Chad J Wrote:


> If the proposed solution is not good enough, please give a better one. 
> Suggestions are welcome and encouraged.
> 

I suggest something similar to python properties, for those who are not familior with it a property is created with property(getter, setter) so with my idea property (or inout or some other keyword) would be a new type, sort of like a function or delegate, but with behavior like that described by Chad.  The syntax would be somthing like:

int getX(){ return x;}
void setX(int i){x = i;}

property int X(&getX, &setX);

the problem is that unlike in python we cannot use keywords to set paramaters so some syntax would have to be made for write only properties, maybe if we used inout instead of property:

//read only
in int x(&getX);
//write only
out int x(&setX);
//read and write
inout int x(&getX, &setX)

any read would call the reading function (.reader of the property object)
any write would call the writing function (.writer of the property object)

so obj.x++

would be the same as:
obj.x.writer(obj.x.reader() + 1)

when read the property will actually return a special wrapper that sends itself to the writer whenever it is assigned to 
the address of the property cannot be used as a function or delegate, but can be used as the address of the type 




More information about the Digitalmars-d mailing list