This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Tuples and Globals in Groovy
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Tuples
I am really looking forward to life with Groovy when tuples are supported.
I always liked working with these guys in other languages (including Perl):
my ($name, $address, @otherstuff) = parseContactsFile();
Most of the time I get around this by trying to return back real things:
contact = parseContactsFile()
println contact.name
however for some things it can be nice... e.g. from:
new File("input.txt").eachLine { line |
s = line.split("\s*=\s*")
println "Name: ${s[0]}"
println "Value: ${s[1]}"
}
to:
new File("input.txt").eachLine { line |
(name, value) = line.split("\s*=\s*")
println "Name: ${name}"
println "Value: ${value}"
}
Globals
A lot of questions from people hacking up simple Groovy scripts result from their assumptions not being met.
A common one is thinking that you can create a variable at the top of a script and have it scoped so you can use it inside methods / closures somewhere down the pike.
Globals are not considered to be friendly folk, but for simple scripting they can be a pleasure.
I just hope that when implemented in Groovy they don't go the TCL world, so we don't see: upvar foo and global foo all over the shop!