My current project (a web application) has a module that is almost entirely written in Groovy. Note: We are using the latest version 1.6.5 of Groovy.
In one of the methods we had some logic similar to the following:
def myMethodToProcessTheList(def listOfThings) {
if (listOfThings && listOfThings.size < 0) {
//Do my logic}
}When the above code is run using Java 5 everything works wonderfully. However when we pushed the code out to our server running Java 6 we got an interesting error stating that it couldn't compare java.util.ArrayList to java.lang.Integer.
The problem is actually that the size method needs the parens after it (Note: The Groovy docs actually do state that methods with at least ONE param can omit the parens). I realize now that it would make sense that parameter-less methods would need parens because how else would Groovy distinguish between methods and property accessors? I did find one forum post that indicated that in Groovy, lists have special properties that act like the spread-dot operator. That is why the listOfThings.size returned the ArrayList instead of a number.
So why does this work in Java 5 but not Java 6? That is a great question, one of which I really don't have a definitive answer. I'm not completely sure if this purely a Java 5/Java 6 difference or simply a difference between the IBM Java 5/Java 6 JVM.
If anyone has a theory as to why it would work one way on Java 5 and the other in Java 6, I would love to hear it.
0 TrackBacks
Listed below are links to blogs that reference this entry: Be careful with Groovy's optional parens on method calls!.
TrackBack URL for this entry: http://www.nearinfinity.com/mt/mt-tb.cgi/1539



Leave a comment