Automatic getter/setter generation

Lutger lutger.blijdestijn at gmail.com
Thu Oct 22 13:14:50 PDT 2009


Saaa wrote:

> I was thinking about using a template which would generate a variable and
> its getters/setters automatically.
> Something like this:
> 
> mixin var!(int,"_name","rwr-");
> 
> // private int _name
> // public int name(){return _name};
> // protected void name(int value){_name=value};
> 
> What do you think about something like that?

I'm not sure it is very useful. I can think of a couple of reasons to define 
simple getter / setters:

- must be done when implementing an interface
- binary compatibility perhaps (again: interfaces)
- you will or might add functionality to the get/set functions at a later 
stage. 

If these are the only reasons, I would just go ahead and implement the 
get/set functions manually. Then again it may be useful depending on your 
application. 

One more thing you could use this for is adding functionality to get/set 
functions:

template DefaultImplementation(T, string name)
template LoggingImplementation(T, string name)
template UndoRedoImplementation(T, string name)

mixin var!(DefaultImplementation(int, "_name")
mixin var!(LoggingImplementation(int, "_name")
mixin var!(UndoRedoImplementation(int, "_name")










More information about the Digitalmars-d-learn mailing list