Actor Christopher Walken once performed a skit on Saturday Night Live in which he proclaimed he needed more cowbell to get rid of his "fever". In the same light, the Groovy team has certainly heard the calls for more Groovy and have recently released 1.6 of their award winning language. There are a slew of new features mentioned in the release including,
- great runtime performance improvements
- multiple assignments - optional return in if/else and try/catch blocks
- AST transformations and all the provided transformation annotations like @Singleton, @Lazy, @Immutable, @Delegate and friends
- the Grape module and dependency system and its @Grab transformation
- various Swing builder improvements, thanks to the Swing / Griffon (http://griffon.codehaus.org) team
- as well as several Swing console improvements
- the integration of JMX builder
- JSR-223 scripting engine built-in
- various metaprogramming improvements, like the EMC DSL, per-instance metaclasses even for POJOs, and runtime mixins
One of the primary focuses in this release was on performance and the Groovy team claims to have made significant improvements ranging from 150% to 460%. Another feature included in this release was the official integration of the JMX builder, which is just another example of community efforts helping to improve Groovy.
Below are some examples of several of the new features including multiple assignments and AST transformations.
// this class' properties are immutable once the object is constructed
@Immutable final class ServerConfig {
String url
int port
}
def getServerInfo() {
['http://home.net', 8080]
}
// attempts to set a property on an Immutable object
def setUrl(config, newUrl) {
try {
config.url = newUrl
}
catch (ReadOnlyPropertyException ex) {
ex
}
}
// multiple assignment
def (url, port) = getServerInfo()
assert url == 'http://home.net'
assert port == 8080
def config = new ServerConfig(url, port)
assert config.url == url
assert config.port == port
// try to change the property on the Immutable object
def result = setUrl(config, 'www.google.com')
// verify the property change failed
assert result instanceof ReadOnlyPropertyException
The example above shows how the @Immutable AST transformation provides a very simple way of creating a read-only object. The example also demonstrates using the new "multiple assignments" feature. For more information about the AST transformations you can visit the Groovy user guide, currently the section only covers the @Immutable transformation.
Now that you've seen a quick overview of what Groovy 1.6 has to offer, check out What's New in Groovy 1.6, an in-depth article about Groovy 1.6 written for InfoQ by the Groovy Project Manager, Guillaume LaForge. Guillaume covers each of the new features and provides plenty of code examples which help clarify all of the new capabilities.