Thursday, December 3, 2009

Calling methods with param name: value

One of the things I like in Groovy is that when I call a method that takes one or more arguments, I can call this method in the caller program with explicit names of the method argument, that way I don't have to remember the order of the arguments.
e.g. If the method is:
public List fetchAccountAdmins(Account account, boolean effective)

Use: fetchAccountAdmins(account: myAccount, effective: true)
instead of: fetchAccountAdmins(myAccount, true)

This is a little verbose but gives me more freedom. However, there is one caveat, the target method you are calling must be implemented in Groovy. It can't be a Java method.