Groovy’s .with() and multiple assignment

Multiple assignment

I recently discovered a relatively little-known feature in Groovy called multiple assignment.  As the name indicates, it allows you to assign multiple values to multiple variables in one statement:

The Groovy docs do a good job of explaining the basics, however I did notice one limitation:

“[C]urrently only simple variables may be the target of multiple assignment expressions, e.g. if you have a person class with firstname and lastname fields, you can’t currently do this:

The key words here are “one simple variable.”  If only there were a Groovy construct that let us do this…

.with()

This is where the .with() method comes in.  It allows you to use methods/properties within a closure without having to repeat the object name each time. It does this by setting that object as the delegate of the closure, so any otherwise “undeclared” variables or methods get invoked on that object.  So if we have something like a Dog class and a Color enum:

we would normally set each property individually with:

And by using .with() we could simplify it to:

However, if we were to try multiple assignment here, it wouldn’t work because of the limitation above:

See how .with() can come in handy with multiple assignment?  The entire block could then be reduced to:

Of course, I’d recommend using this feature sparingly, as your code may be a little harder to read, but it is a potential workaround for assigning multiple values to properties!

Igor Shults

About the Author

Object Partners profile.

One thought on “Groovy’s .with() and multiple assignment

  1. Chris Malan says:

    This is neat. I use Groovy in Grails. Don’t think this isn’t appreciated.

    1. More appropriate here is probably Groovy’s map constructor:

      class Person {
      String firstname
      String lastname

      String toString() {
      “$firstname $lastname”
      }
      }

      Person me = [firstname: “Chris”, lastname: “Springer”]
      println me

      For larger objects with many properties, the builder pattern may be even more appropriate. Simple implementations can use @Builder annotation as of Groovy 2.3 to create a basic implementation with any code.

  2. Igor Shults says:

    @Christopher — Yeah the example here was pretty simplified. If you’re literally setting properties immediately after (or while) initializing an object, then this isn’t the best approach. But if you say, are performing an update on multiple properties of an object, then it could be more useful.

  3. Chuck Johnstone says:

    It’s a good solution when you want to assign from an array:

    def csv = ‘first,last,age’

    person.with {
    (first,last,age) = csv.split(‘,’)
    }

    Is a very useful technique.

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]