Here is an example similar to the method I had written:
def details(full_name, address, contact_numbers)
{
// ignoring stuff to process data
full_name.append('Venkat Subramaniam')
address.append("Venkat's address")
contact_numbers << 'phone_number_1'
contact_numbers << 'phone_number_2'
contact_numbers << 'phone_number_3'
}
To use the above method I had to write something like:
def full_name = new StringBuilder()
def address = new StringBuilder()
def contact_numbers = []
details(full_name, address, contact_numbers)
Today, I got around to refactoring my code, thanks to the Groovy 1.6 (beta 1) facility to return multiple results from a method. The above example,
refactored looks like below:
def details()
{
// ignoring stuff to process data
['Venkat Subramaniam', "Venkat's address", ['phone_number_1', 'phone_number_2', 'phone_number_3']]
}
And I can call it as follows:
def full_name, address, contact_numbers
[full_name, address, contact_numbers] = details()
Simple, less noisy, elegant, and my refactored code does not look (that) ugly anymore.
In Groovy 1.6 (beta1), if a method returns an ArrayList, you can assign it to an ArrayList made up of variables you'd like to assign the values to.
If there are fewer elements on the lhs, the extra values in the result are ignored. If there are more elements, the extra elements in the lhs are assigned null.
I think this is nice step in the right direction. I hope there is a way to capture the extra elements on the rhs into an array.