Brian's Waste of Time

Fri, 31 Oct 2003

Forays into Groovy

After my last real post (NFJS reactions) multiple people, including James Strachan and Bill de Hora, told me about Groovy. I had seen reference to it on James's blog and nosed around the wiki before, but it didn't lead anywhere. With all of the prompting I figured I needed to give it a good look over

I really like what I see. When I looked over the wiki previously I got an impression that they were cherry-picking from Python and Ruby and I didn't think the language was really being built around a strong set of design principles. I have learned the error of my ways, largely from chatting with James and Bob Mcwhirter in irc while trying to figure things out.

I started with a canonical "Hello, world" in the form:

class Test { void main() { println("Hello World") } }
This was based on code from the wiki. Running it proved a bit tricky (that is when I hit irc) until I was kindly told that it still requires boot-strapping from Java to get started but that wasn't too bad. A side effect of the boot-strapping process is that after I posted the boot-strap to the wiki in an original form of using a List to collect the command line args, Bob told me that Groovy doesn't care if it is a List or Array, it treats them the same. Good! However, "Hello world" errors out. James, being a brit, had long ago gone to bed and Bob is a self-described "the compiler guy" so I was left scratching my head and patching Groovy to behave like the wiki says it should (easily done, the code is beautifully factored and layed out).

I play some more, pop up some Swing stuff, work out how it differs from Ruby closure style:

["1", "2", "3"].each { foo | println(foo) }
compared to
["1", "2", "3"].each { |foo| puts foo }
etc. Digging around some more a *lot* of the ruby/perl/python convenience idioms have been placed on the GroovyObject. It is nice.

Next morning I hop into irc again and sure enough James and Bob make time to help out. I mention the "hello world" problem and James tells me it is by design. The proper form is

class Test { void main() { "Hello World".println() } }
Which makes my brain hurt for a moment. Using Java constructs is legal so you can still call System.out.println(); with impunity, but println() has been made a function of the base object (GroovyObject) in the language. Groovy is very oo'ish in the sense that Smalltalk, Ruby, and Objective-C are very oo'ish. I like this as it maps nicely to how I think.

James describes the language as a dynamically typed, ruby-like language built on how the JVM works. That last part is an important incite into some of its bits. Ruby is built on C as a standalone tool. Groovy is specifically designed to enable Java developers to feel at home, and to interact well with Java. You can use groovy objects in Java, access their properties, methods, etc reflectively very easily. It playes nicely with Java and feels like message-passing oriented Ruby while still being a method-calling style language like Java. I like it, a lot.

Anyway, much discussion ensued and James wound up patching Groovy to accept println(...) as a global function. He had convinced me it shouldn't be done, but c'est la vie. I half think he did it to avoid having to change all the samples on the wiki (side not, he is a genious in what he wants to do with the wiki for documentation purposes, more on that in some other entry later).

Going back to the Java-centric/compatible design: Groovy used to manipulate and build on Java based tools seems to be very elegantly done. It uses extent Java technology as underpinnings for much of its functionaliy in ways that let you get to the lower level Java stuff through the abstractions. I haven't played with the JMX based remote objects implementation yet, for instance, but am excited about it (I don't know if that makes sense or not, I have only seen JMX as the control side of containers, have never had need/desire/itch to hack it -- now I might).

I need to write a web-based ldap administration tool in the near future so think I will use Groovy (and the Novell LDAP library) to give it a go and see if I can pare down typical Servlet folderol a bit. Will post as I learn.

1 writebacks [/src/groovy] permanent link