Groovy 4

Back in the Groovy 4

Rik Scarborough Development Technologies, Groovy, Java Leave a Comment

For many years now, my go-to language for rapid development and scripting has been Groovy, especially if the task involves any type of database access. I’ve been coding in Java since the mid-’90s. Groovy is a natural extension of that experience.

Being so familiar with both languages does have one drawback; I tend to use the features in Groovy that I have always used. I don’t branch out all that often.

However, when I heard that Groovy 4 is coming out, I decided to get ahead of it. I went online to explore what new features we were getting and how I could start using them in my own code.

This article will not be a full list of the new features of Groovy 4; there are simply too many for one post. If you want, though, you can find that information here! This post will simply be my thoughts on a select few of those features and how I would plan to use them.

Let’s get started!

Switch Expressions

The switch expression feature will allow us to evaluate a statement based on a case branch, basically giving us a variable = switch(case) ability. It’s new to Groovy, but the truth is that this is borrowed directly from Java. As I use Groovy for scripting, this provides a more concise way to do tasks such as menus.

Here’s an example. I have a script for posting work tasks to a web service. I have a menu item in the script to choose between a Meeting or Project task, so I can type in a single digit instead of typing out the whole category each time. I could use this new expression to quickly capture that.

print "Categories (1 for Meetings, 2 Project:) " 
def cat = switch( as Integer) { 
case 1 -> "Meetings" 
case 2 -> "Project" 

This is a simple example, but more complicated use cases exist where this handy little feature would save quite a bit of time and effort. I’m happy to see this!

What’s next?


Groovy 4 will be bringing in a record-like feature, borrowing again from recent versions of Java.
This gives us a quick way to write a data-driven object. Here’s an example.

record Task(String category, String description, String project, Integer timespent) { } 
def t = new Task("Meetings", "Daily Standup", "Working Project", 30) 

This is useful for making domain objects clearer, smaller, and repeatable. Tasks that
create multiple objects would be especially useful.

while (input != null) {
    recordList << new Task(input.tasktype, input.description, input.project, input.timespent)

I really hope this shows up in Groovy SQL.

Built-In Type Checkers and Macro Methods

Groovy has long had a type-checking mechanism and macros, but this is different. In this release, the intention is to begin to add pre-built type-checkers and macros to the distribution.

I have to admit, at this point I don’t use either feature, but if they provide some really useful built-in ones, I will likely begin to use them. I can also see this motivating me to write my own down the line…

Cool, right?!

POJO Annotation

This is another incubating feature. Although I find it ironic that we would have an annotation for a Plain Old Java Object, knowing that Groovy adds a lot of enhancements to objects, I can see the necessity in writing code that might have to interact with Java. Once the Groovy code is compiled, there will no longer be a need for having the Groovy jars available to the Java compiler. Helpful.

Groovy Contracts

Here’s a hot take, I’m not a huge fan of using annotations for coding logic. However, a friend of mine calls it magic programming. You annotate an object, and code is magically added, so you don’t have to add things like val > 0 || throw Exception.

I can see how useful annotations are during rapid development. Looks like Contracts will add a few annotations that will support items such as class-invariants, pre-conditions, and post-conditions. This might be extremely valuable in writing and communicating with web services.

Groovy-Integrated Query (GINQ)

Ok, this is the one I am the most excited about. This new feature will give us the ability to query collections in an SQL-like manner. Writing code to analyze data is a big part of what I do daily. I think this and the Record addition are going to be powerful.

That was brief, but I really am looking forward to having this ability. Look for more blog posts from me on this in the future!

Other improvements

I’m happy to see some of the other improvements, especially the GString performance improvements. I have to think that the lack of widespread adoption of the Groovy language, especially in Java shops, has at least a little to do with overall performance. Hopefully, these updates will change things.

It seems small, but the change to not requiring a leading zero on a decimal value makes me happy. It’s the little things, right?!


As I said in the beginning, this is nowhere near a comprehensive list of the changes coming in Groovy 4. It’s just a quick thought on the ones that stick out to me.

Also, I appreciate knowing that Groovy is an active language. I will continue to use it for my own projects and evangelize it with the teams I work with.

If you are a Groovy user, I hope you look at the improvements coming. If you’re not using Groovy, I hope this post gives you the encouragement you need to take a look, especially if you are a Java programmer. I don’t think you’ll regret it.

As a matter of fact, I’ll help you get started. I wrote a couple blogs on Groovy about 5 years ago. Things have changed (obviously), but it might be a good spot to start! Check them out – Making Spring Batch Groovy and Switch to Groovy for Unit Testing.

Believe in Good Code.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments