At first glance, Groovy’s scope rules are much like Java’s. A variable name
will refer to a local variable, a variable from a containing scope, or the
superclass or -interface of a class scope, or possibly from static
imports. Except in contrived cases, the system is fairly easy to reason about,
and in any case the compiler will catch you if you do something wrong.
Groovy, of course, had to take a decent system and try to make it more
“dynamic”. In some ways, the scoping system is more like Python, especially in
that variable existence is determined partially at run-time. Python’s rules,
however, are fairly simple, and make that system basically work.
Today, we’ll be talking about how Groovy doesn’t seem to have a consistent idea
of what an identifier is. It’s not a very large topic, and it really isn’t a
problem, but it is a topic that nevertheless merits mention, and it allows me
to introduce…
The first look at any programming language often begins with the traditional
“hello world” program. Below is Java’s, for reference.
HelloWorld.java
1public class HelloWorld {
2 public static void main(String args[]) {
3 System.out.println("hello world");
4 }
5}
Groovy’s, on the other hand, looks like this:
HelloWorld.groovy
1class HelloWorld {
2 static def main(args) {
3 println("hello world")
4 }
5}
Gr- — (Javanese) Agglunative prefix, typically indicating
expectation of a painful experience, often due to lack of forethought
or impropper planning.
Example constructs include:
Groovy — Adjective. Characterised by a large number of hacks
attempting to make things better, but which really only increase the
number of problems by an order of magnitude. “That’s some groovy
code you’ve got there. I especially like how it uses the query
parameters to dynamically select the class and static method to
execute.”
Grail(s) — Verb (almost always found in the singular present). To
inflict suffering and confusion by means of unnecessary complexity, self
contradiction, and annoying surprises separated by excrutiating waits. “It
really grails me how this ‘convention over configuration’ framework
requires three multi-thousand-line configuration files to run.”
Gradle — Noun. An obstensibly comfortable, metaphorical location in
which something is cradled before finally succumbing to death. “After having
been in gradle for a few months, we found that the code had become
impossible to update, due to library version conflicts.”