Groovys .with() and multiple assignment

Groovy has a limitation that restricts multiple assignment to simple variables. However, by using with(), we may be able to work around that..

Igor Shults

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:

def (foo, bar) = ['foo', 'bar'];
assert foo == 'foo'
assert bar == 'bar'

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:

(p.firstname, p.lastname) = "My name".split()

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:

class Dog {
    Integer age
    String name
    Color color
}

enum Color {
    BLACK,
    GOLD,
    WHITE
}

we would normally set each property individually with:

Dog dog = new Dog()
//...
dog.name = 'Rex'
dog.age = 2
dog.color = Color.BLACK

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

Dog dog = new Dog()
//...
dog.with {
    name = 'Rex'
    age = 2
    color = Color.BLACK
}

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

Dog dog = new Dog()
//...
(dog.name, dog.age, dog.color) = ['Rex', 2, Color.BLACK] // Does not work

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

Dog dog = new Dog()
//...
dog.with {
    (name, age, color) = ['Rex', 2, Color.BLACK]
}

assert 'Rex' == dog.name
assert 2 == dog.age
assert Color.BLACK == dog.color

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

Share this Post

Related Blog Posts

JVM

Gradle Summit 2014 Recap

June 18th, 2014

Review of material and presentations from Gradle Summit 2014, Santa Clara, California.

Object Partners
JVM

Inline initialization of Java Maps

June 5th, 2014

Inline initialization of Java Maps is never pretty, this solution provides a type safe and efficient way to do so.

Neil Buesing
JVM

Run Grails Commands on Heroku

May 22nd, 2014

Instructions on how to run Grails commands on Heroku using an example of how to run Grails migrations on Heroku

Brandon Fish

About the author

Igor Shults

Sr. Consultant

Igor is a self-driven developer with a desire to apply and expand his current skill set.  He has experience working with companies of all sizes on the whole application stack, from the database to the front-end.  He enjoys working in collaborative environments involving discussion and planning around data modeling and product development.