Categories
java

Gettin’ Groovy…

Yeah, I know my post title is lame, “Gettin’ Groovy.” That it took no real imagination at all to come up with. That it’s an overused. Blah, Blah, Blah… My response is that you don’t name a programming language, Groovy, if you don’t want to see these corny uses of its name…

Anyhow, I do dig the name, and the language. I tried to spend some time, earlier in the year “Gettin’ Groovy,” but just got to busy with work. I attended a couple awesome talks at JavaMUG on Groovy, and then Grails, and was sold.

So here I am. Trying to really understand the language. Why it works the way it does, versus just hacking-away with it. I’ve bought the book, “Groovy Programming – An Introduction for Java Developers” which covers a lot of details about the language. Stuff you wouldn’t pick up while reading a web post about Groovy. Stuff you need hunderds of pages to fill a book on.

My only complaint with the book so far is that I’ve found no way to communicate with the book’s authors. The two websites mentioned in the front of the book appear to be dead. I checked them many times over the weekend and repeatedly got timeouts.

So my first observation on the language is that I don’t understand why parentheses are optional. It makes it hard to learn a language when you don’t know if you have a bug in your code or just are missing a set of parens.

Take the simple println statement. Unlike Java, you can simply say:

println “some text”
println “some *more* text”

The problem is when you decide you want a blank line between the two stmts:

println “some text”
println
println “some *more* text”

This throws the generic error…

some text
Caught: groovy.lang.MissingPropertyException: No such property: println for class: test
at test.run(test.groovy:2)
at test.main(test.groovy)

I guess that error isn’t that confusing, now that I’ve been using Groovy for a couple days. However, here is another case where the missing parens hurt a bit. I’m exploring the Java base classes and how they work in Groovy. One case where I’m trying to understand the fact that all numbers are either Integers or BigDecimals. Not too hard, but I’m trying to prove it to myself with code like this…

def x = 0.123
println x
println x.getClass

It’s hard to prove something like this when I get the following output:

0.123
Caught: groovy.lang.MissingPropertyException: No such property: getClass for class: java.math.BigDecimal
at test.run(test.groovy:8)
at test.main(test.groovy)

The error message is similar to the one I got when using println, but the question I have is, is Groovy telling me I don’t have access to the java.lang.Object.getClass() or is Groovy telling me I have to just use the parens?

Why not just make the dang parens required? Isn’t there something to be gained by some consistency?

Leave a Reply

Your email address will not be published. Required fields are marked *