Search

Dark theme | Light theme

September 14, 2009

Groovy Goodness: Named Parameters are Converted to Map

We can use named parameters when we invoke a Groovy method, but Groovy doesn't invoke the method with just those parameters. Groovy collects all named parameters and puts them in a Map. The Map is passed on to the method as the first argument. The method needs to know how to get the information from the Map and process it. There is one rule: the first parameter of the method must be the Map parameter, because Groovy will put the Map as first argument in the method invocation. Other parameters are appended in the same order as defined in the method invocation.

class Address {
    String street, city
    int number    
}

class Person {
    String name
    Address address
    String phoneNumber
    
    def move(newAddress, newPhoneNumber) {
        address.street = newAddress.street
        address.number = newAddress['number']
        address.city   = newAddress."city"
        phoneNumber = newPhoneNumber
    }
}

def a = new Address(street: 'Main St.', number: 1, city: 'Duck City')
def p = new Person(name: 'mrhaki', address: a, phoneNumber: '555-123499102')

p.move(street: 'High Av.', city: 'New Yark', '00920120', number: 42)  
// Groovy transform the method invocation to:
// p.move([street: 'High Av.', number: 42, city: 'New Yark'], '555-00920120')

assert 'High Av.' == p.address.street
assert 42 == p.address.number
assert 'New Yark' == p.address.city
assert '555-00920120' == p.phoneNumber
assert 'mrhaki' == p.name