Mark Watson on automatic accessors for Java
Mark Watson proposes a change to the Java language, in the form of making it possible to automatically generate accessor methods for class attributes. It would work a bit like the generic functions created by the :reader
, :writer
and :accessor
slot options given to defclass
in Common Lisp.
However, I think that if that part of the language is to be changed — and I agree it should be — the new design should go all the way to hiding the methods from the users of the class. It should be possible to optionally define the accessor methods you want and then have them accessible by simply using the attribute name. So object.attribute = value would invoke object.setAttribute(value) behind the scenes, if such a method was defined. For backwards compatibility, it would need to do the normal assignment if it was permitted and no method was defined. A close relative to look at for inspiration would be C#. This one of those places where the language is, simply put, better.
[UPDATE]
Reading Bruce Eckel's great blog, I stumbled across this: The Java team is clearly not ignoring the threat of C# [...] along with the new features already on the list for "Tiger" (JDK 1.5), including true enumerations, autoboxing, and generics (templates), they are adding attributes, something (along with autoboxing) taken directly from C#, because it's a good idea.