Tuesday, September 6, 2011

Groovy's Special Words

The Java Language Keywords are words that cannot be used "as identifiers in your programs." The Java Tutorial states regarding Java keywords, "The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs." In other words, the greater set of words that cannot be used as identifiers in Java code are "reserved words" including the keywords plus true, false, and null.

It is not surprising that Groovy "inherits" Java's reserved words. However, Groovy adds some of its own as documented in the Reserved Words section of the Groovy User Guide. This section highlights that Groovy adds the following words to the set of Java reserved words: as, def, in, and threadsafe. Three other words that I treat as "reserved words" when writing Groovy scripts are the closure-related implicit names delegate, it, and owner. Other special Groovy "words" are use and with provided by Groovy's Object class. I cover these nine "special" Groovy words in this post.


it, delegate, owner

The three keywords it, delegate, and owner are used with Groovy closures. They provide handles to implicitly available objects from within the closure's body. Although these keywords can be used outside of the closure body for other purposes, I tend to reserve them for use within a closure. In fact, one can even use them for purposes other than their intended purpose within a closure body, but this seems to be especially poor taste to do.

For closures accepting a single argument, the keyword it can be used to represent that single parameter rather than explicitly specifying the single parameter's name. The keywords this, owner, and delegate also have special meaning within a closure.


as

There are multiple uses of the as keyword in Groovy. In The Groovy 'as' Keyword, Daniel Silva provides examples of using the as keyword to coerce a type (prettier cast) and to create an alias for an import statement (as of Groovy 1.5). Steven Devijver's post Higher-order functions with Groovy, part 2 talks about using the as keyword used "on closures to turn them into interface implementations" using java.lang.reflect.Proxy.


in

The in keyword is sometimes used in Groovy to ascertain whether an object is contained within a collection. A nice example of this is demonstrated in the post The Groovy 'in' Keyword. The in keyword is often used in Groovy in conjunction with iteration/looping. The StackOverflow thread How does the Groovy in operator work? states that "it looks like the in operator is based on the isCase method" and that Groovy's collections' contains() methods are called by isCase.


def

The def keyword is frequently seen in Groovy code listings. The What is this 'def' I've Heard Of section of Scoping and the Semantics of 'def' in the Groovy User Guide has this to say about def:
"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.
This documentation also specifies a rule of thumb for thinking about def: "think of 'def' as an alias of 'Object'."

There are some nuances with the use of def depending on whether the Groovy code is a script or a class. I blogged on this in the post Visible Script Variables: Using Groovy 1.8's @Field AST in which I showed how Groovy 1.8 made it possible to use def in Groovy scripts and still have the defined variable available to methods on the script.


threadsafe

I was curious about threadsafe when I saw it on the list of Groovy reserved words because I did not recall seeing it in any code samples. It turns out there's a reason for this: Guillaume Laforge stated that the threadsafe keyword is a "reserved word we've never used so far" (16 August 2007).


use

The use keyword is applicable when using Groovy Categories as illustrated in the Pimp My Library Pattern example and as illustrated in my concisely named post Adding Common Methods to JAXB-Generated Classes (Separate Class/Groovy Categories).

The use "keyword" is actually NOT a keyword, but is a method on Groovy's GDK extension of the Object class and is provided via Object.use(Category, Closure). There are numerous other methods provided on the Groovy GDK Object that provide convenient access to functionality and might appear like language keywords or functions because they don't need an object's name to proceed them. I tend not to use variables in my Groovy scripts with these names (such as is, println, and sleep) to avoid potential readability issues.


with

The Groovy GDK Object provides another method that I want to focus on here. The with(Closure) method allows us to group method calls and property accesses to a single object.


Conclusion
I have covered special Groovy words in this post that are either reserved words (in addition to Java reserved words) or are special handles by which greater functionality is invoked in Groovy. In general, I avoid using these words as identifiers or as names of my own methods. Although some of them are allowed for such uses,I generally avoid doing this to maintain readability. Groovy even allows for Java keywords to be overridden by method names, but I likewise don't typically take advantage of this.

1 comment:

@DustinMarx said...

Groovy Closures: this, owner, delegate let's make a DSL covers three keywords and their use in closures in detail.