Dark theme | Light theme

May 20, 2010

Groovy Goodness: Multiple Assignments Revisited

Multiple assignments in Groovy is something already covered before in the Groovy Goodness series. But today at the Gr8Conf I got some extra information about it's possibilities. First of all we can use type information in the multiple assignment. So we can assign the returning values to typed variables. The next thing is we can use multiple assignments basically for anything with a getAt method. And this works also for our own classes, we only have to provide an implementation for the getAt method.

class Size {
    int x, y

    Object getAt(int index) {
        index == 0 ? x : y

def (int myX, int myY) = new Size(x: 12, y: 30)
assert 12 == myX
assert 30 == myY