Truly scary: Just In Time Groovy Libraries WebLogic 8.1 Platform named “far from revolutionary” in class action law suit against BEA
Jul 22

Tuples and Globals in Groovy

Groovy, Tech Add comments

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!

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'