jastrachan | 1 Apr 16:39 2004

Syntax sugar for Java Beans property declaration (was Re: Mike on Properties)

On 31 Mar 2004, at 23:54, Cedric Beust wrote:
>  Well, thanks Mike for finally delivering.
>  I posted a few comments on your analysis in my weblog, but at this 
> point, I think we should take the discussion on this list.  It looks 
> like the JSR EG has their work cut out for them :-)

I'd like to focus in this thread not on the use of Java Beans in Groovy 
but how we define them in Groovy - other than the standard Java way of 
writing getters & setters. i.e. the aim is to create bytecode which any 
Java code can introspect to see Java Beans compliant properties (and 
events too but we've not looked at that yet).

Right now we've used the simple rule that its bad form to use public 
fields - fields should be private to avoid exposing your 
implementation. So that declaring a public field is equivalent to 
defining a Java Bean property. If you want to make a read only 
property, declare a private field & getter or a public field & 
private/protected setter.

Its a very simple model which seems to work surprisingly well (so far).

One issue is that we don't allow public fields to be created in Groovy. 
I personally don't think this is such a bad thing as switching from a 
field to property breaks binary compatibility anyways but I guess its 
an issue. So maybe properties & fields should have explicitly different 

We could consider some C#-like syntax sugar to make adding a 
getter/setter easier...


Or there's new keywords like Mike suggested.  Another approach is to 
use macros - like Ruby does - to add properties. I confess to sitting 
on the fence with macros so far - I think they are cool & useful, 
though they make refactoring really hard so would rather avoid 
requiring them until we know that we absolutely need them and that it 
won't break refactoring support.