www.digitalmars.com         C & C++   DMDScript  

D - casting overloads and properties

I was thinking that if D supported overloading of casting, and 
assignment operators like c++ then it would be really easy to implement 
properties as part of a library, rather than as part of the actual 
language spec. This would allow me and I'm sure others to have their 
properties and Walter wouldn't have to implement them in the compiler. 
There are a ton of other uses for casting operator overloads, but this 
is the one I'm mainly interested in at the moment.  I actually thought 
that the assignment operator could be overloaded, but when I went to 
look for the function name I didn't see it.  If these things can already 
be done, please tell me.  I propose something like this:

template TBaseProp(T)
{
     class DBaseProp
     {
     protected:
         T prop;
         T getter(){return prop);
         T setter(T newval){return prop=newval);
	T delegate() getfunc;
	T delegate(T) setfunc;
     public:
	this()
	{
	    this(&getter, &setter);
	}
	this(T delegate() dget, T delegate(T) dset)
	{
	    getfunc=dget;
	    setfunc=dset;
	}
	T ass(T newval)  //for assign, as in = not the other thing :-)
	{
	    return setfunc(newval);
	}
	T cast()    //cast overload of course
	{
	    return getfunc();
	}
     }
}
instance TBaseProp(bit) bitprop;


now it's easy to create properties for any fields in a class, and 
implementing custom handlers for them is as easy as writing the handler 
and changing the one line of code that calls the properties constructor.
of course some more operators would need to be overloaded, a couple more 
constructors are needed and a few more templates (numeric and array 
properties), but that's all just as easy.

What do you all think?  Good Idea, Bad Idea?
Feb 14 2003