digitalmars.D.learn - Automatic getter/setter generation
- Saaa (11/11) Oct 21 2009 I was thinking about using a template which would generate a variable an...
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? Also, it would be nicer if it would take: mixin var!(int _name, "rwr-"); But I don't know how to get the name that way.
Oct 21 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")
Oct 22 2009
Lutger wrote: ...mixin var!(DefaultImplementation(int, "_name") mixin var!(LoggingImplementation(int, "_name") mixin var!(UndoRedoImplementation(int, "_name")Sorry, these would be: mixin var!(UndoRedoImplementation, int, "_name") etc. where UndoRedoImplementation is an alias
Oct 22 2009
Lutger wrote: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.I thought about using something like this because especially in larger classes I tend to forget how certain variables are defined and this way it would all be nicely packed at the top of the class (still fitting within a single screen).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")Undo Redo, I think I've never had the use for those but maybe I'll let them in my consciousness for a while :)
Oct 22 2009