gotoare reserved, even though they are not currently used.
nullmight 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
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:
threadsafe. Three other words that I treat as "reserved words" when writing Groovy scripts are the closure-related implicit names
owner. Other special Groovy "words" are
withprovided by Groovy's
Objectclass. I cover these nine "special" Groovy words in this post.
it, delegate, owner
The three keywords
ownerare 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.
There are multiple uses of the
askeyword in Groovy. In The Groovy 'as' Keyword, Daniel Silva provides examples of using the
askeyword 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
askeyword used "on closures to turn them into interface implementations" using java.lang.reflect.Proxy.
inkeyword 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
inkeyword 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
inoperator is based on the isCase method" and that Groovy's collections'
contains()methods are called by
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" 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
defdepending 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
defin Groovy scripts and still have the defined variable available to methods on the script.
I was curious about
threadsafewhen 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
threadsafekeyword is a "reserved word we've never used so far" (16 August 2007).
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).
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
Objectthat 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.
The Groovy GDK
Objectprovides 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.
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.