Dec 24th 2008 Groovy Spread Operator Optional For Properties (plus: A Peek Into The Sausage Factory)
So groovy has this cool operator called the “spread” operator: “*.”.
Right now, it’s listed as the top hidden feature of Groovy over on StackOverflow. It works like this:
[1, 2, 3, 4]*.toString() // equals ["1", "2", "3", "4"]
It applies the method/property to the right of the operator to each member of the collection and returns the results as another collection. It’s syntactic sugar for doing this:
[1, 2, 3, 4].collect { item -> item.toString() }
(and that’s syntactic sugar for a whole pile of Java code :)
Did you know that for properties, you don’t actually need to use the spread operator? Neither did I till I fat-fingered a command working with a list of Grails domain objects and it still worked.
Continue Reading »
1 Comment » Posted by tednaleid / grails and groovy