Groovy

I saw Groovy on Grails awhile back. It's a Java framework that imitates Ruby on Rails. I wasn't massively impressed at first with Ruby on Rails; it just seemed like a bunch of scripts to supplement a Python like language to build simple websites quickly. My focus is more complex websites, so I moved on after awhile, with a few lessons learned. However, I should have given Ruby a look separately from Rails, and taken into account that not much does what Rails does, either.

Anyways, SpringFramework bought Groovy on Grails. I use Spring daily, and decided to take a look at Groovy and Grails to see what might be in store for Spring. For now, I'm focusing on just Groovy, and wow, I missed the boat on Ruby. It's an alternative language to Java that works with the JVM, it uses mostly Java syntax and is almost fully backwards compatible, while removing an large number of small headaches in the process. It's pitched as both a Java replacement and scripting language.

Basically, take all the lessons learned by Python, and apply them to the existing Java syntax.

Semicolons are optional. Parenthesis are optional if it doesn't introduce ambiguity. The most common packages are automatically imported. == means .equals(), and for non-primitive ==, you use another operator. There's native syntax for Maps and Lists, as well as native regexp. Beans are their own class, with built in features, called an Expando. Instead of calling class.member.method(), you can call class.?member.?method(), which returns null instead of a NullPointerException if member is null. This seems a wonderful hack that will save a lot of code. There's a huge number of helper functions built into the base Java functionality. Primitive arrays behave a lot like collections (.size(), for example). Date has a format() function built in. Strings have much of the Apache Commons StringUtil built into them. Collections do autoboxing; putting a primative int into a collection automatically dumps it into the nonprimitive Integer class.

There are language constructs called closures, which are methods that can be passed as arguments to other methods. They seem to be the non-cosmetic change. This reminds me of ML, and I'm not sure I like it in Java, as it could lead to an enormous amount more complexity. That said, it could possibly also lead to mathematically provable code, which isn't a bad thing.

Both dynamic and static typing of variables, so you don't need to declare a type if you so wish. You can do JavaScript-style variable definitions. This also seems like it could lead to problems.

All in all, this looks like something I'd *really* like to try, given a reason. Next up, I'll take a look at Grails.

No comments: