www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Fixing D's Properties

Christopher Wright Wrote:


 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 

The address of the property should be an address to a property struct, whose getters and setters would work as usual. Hm... --- class Property(T) { T* value; public void opIncrement() { *value++; } public void opDecrement() { *value--; } public T opAdd(U) (U addend) { return *value + addend; } // ... } class MyClass { private: int _x; public: Property!(int) X; this() { // slightly ugly... X = new Property!(int)(&_x); } } --- If you could overload the dot operator as well, then this and some template magic with __traits would give you everything you want. Without the dot operator, you get everything you need anyway for basic types. And classes are already taken care of. Structs are left out, though.

The problem with that approach is it would be difficult to specify any special behavior for the properties (which is an important feature of properties), or am I just missing something. Actually something more like: ---- struct Property(Type){ Type delegate() getter; void delegate(Type) setter; Type opPostIncrement(){ scope temp = getter(); setter(getter() + 1); return temp; } etc... } would work pretty well
Aug 19 2007