Groovy Spread-Dot Operator
I am mainly linking this because I find myself never remembering it and never able to find a reference to it. However, I love this syntax.
Groovy adds some nice operators to the language to write brief code. We already learned about the Elvis operator and the Spaceship operator. And now we see what the spread-dot operator is about and what it does.
link: Groovy Goodness: The Spread-Dot Operator – Messages from mrhaki
// Create a list with 3 objects. Each object has a lang
// property and a say() or sayIt() method.
def list [
new Expando(lang: 'Groovy', say: { "I am Groovy"}),
new Expando(lang: 'Java', sayIt: { "I am Java"}),
new Expando(lang: 'Scala', say: { "I am ${lang}"})
]
// Use the spread-dot operator to invoke the say() method.
assert ['I am Groovy', 'I am Scala'] == list*.say()
assert ['I am Java'] == list*.sayIt()
assert ['I am Groovy', 'I am Scala'] == list.collect{ it.say() }
// We can also use the spread-dot operator to access
// properties, but we don't need to, because Groovy allows
// direct property access on list members.
assert ['Groovy', 'Java', 'Scala'] == list*.lang
assert ['Groovy', 'Java', 'Scala'] == list.lang
About this entry
You’re currently reading “Groovy Spread-Dot Operator,” an entry on _mindMeld
- Published:
- 8.30.09 / 12pm
- Category:
- Groovy
- Tags:










View Comments
Jump to comment form | comments rss [?] | trackback uri [?]