Dennis Laumen We who cut mere stones must always be envisioning cathedrals

Groovy Gems: Multiple Assignment

I've recently started a new job at IHomer. Besides the differences in organization with my former employer they also use a couple of technologies I wasn't exposed to before. One of these technologies is the Groovy programming language, a dynamic language which runs on the JVM. As a Java refugee there's a lot of new stuff here (although I've seen most of it in other languages in my spare time).

Yesterday, Groovy's support for multiple assignment made me happy. I was receiving some comma-separated data from another system which needed to be parsed. In Java this would've required a couple of verbose lines of code.

public class MultipleAssignment {
    
    private String a;
    
    private String b;
    
    private String c;
    
    public MultipleAssignment(String input) {
        String[] splitInput = input.split(",");
        a = splitInput[0];
        b = splitInput[1];
        c = splitInput[2];
    }
}

The Groovy version of the above code is a lot more succinct!

class MultipleAssignment {

    def a

    def b

    def c

    MultipleAssignment(input) {
        (a, b, c) = input.split(',')
    }
}

def ma = new MultipleAssignment('1,2,3')

assert ma.a == '1'
assert ma.b == '2'
assert ma.c == '3'

It's not a massive language feature and I know a lot of languages which support similar constructs. It's always been the small things that make me happy though, and that single line of code made me really happy yesterday.