jastrachan | 5 Apr 08:45 2004

Re: Properties

On 3 Apr 2004, at 21:04, Mike Spille wrote:

> At 12:43 PM 4/3/2004 -0700, Rod Cope wrote:
>> I've got some 400 line Groovy classes and I can't say that I've ever 
>> made
>> the mistake of creating a method that looks like a setter, that 
>> conflicts
>> with an auto-generated setter, and that does something else entirely. 
>>  In
>> fact, it's hard to imagine that I've ever done that in Java, either.  
>> I
>> can't even imagine circumstances that would cause a programmer to 
>> write that
>> kind of code.  Can you give a concrete, real-world example?
> Trivially, the common example is overloaded set* methods with 
> different types, like:
>         setNode (NodeRoot)
>         setNode (LeafNode)
> or:
>         setAmortization (double percent);
>         setAmortization (List values);
> In addition - in Java if you override a method with an exact 
> signature, you mask it.  In Groovy, if you provide a set* or get* that 
> happens to match a property, you automatically (and possibly without 
> realizing it) change the behavior of the '.' method.

If you did either of the above in Java code then your bean would no 
longer work with J2SE or any other bean-enabled software. This is a 
Java Beans issue that Java developers need to be aware of. I don't 
think its Groovy's job to wrap developers in cotton wool to avoid them 
hitting this issue.

So far we seem to be gravitating towards an explicit property keyword 
which enables the auto generation of the getter & setter. If we go with 
this then its up to end users to choose to use this or not. If they 
don't understand what the property keyword does or what Java Beans are 
(we can add warnings in the user guide) then they can keep away from 
this feature and just write Java-like methods & fields.

Sounds like a reasonable compromise to me.

I'm less convinced by readonly / write only syntax though; I can't help 
think its easier to just write getters & setters. It does seem like a 
possible use case for macros though I guess.