Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
[ 51 ]

The Groovy Language


In this chapter, we will conduct a whistle-stop tour of the Groovy language. We don't
have the scope in this book to cover the whole language in a tutorial fashion, but by
the end of the book, we will have covered most of the aspects of the language that
you need to be able to write your own Groovy-based DSLs. For now, in this chapter,
we will just touch on some of the main points that differentiate Groovy from its
parent language—Java.


This chapter makes extensive use of Spock style test assertions throughout. If you
have not already read Chapter 3, Essential Groovy DSLs, I would strongly suggest you
at least read the Spock tests section of that chapter and familiarize yourself with the
unique Spock syntax.


Introducing the Groovy language


In the following sections, we will cover some of the fundamental concepts and
features of the Groovy language. A working knowledge of Java is assumed, so we
will focus on what is different between the Groovy and Java languages.


The module structure


Groovy programs and scripts are generally stored in Groovy source files with the
.groovy extension. The exception to this are the Unix "shebang" scripts described
in Chapter 2, Groovy Quick Start. Unlike Java source files, which must always contain
a class definition, Groovy source files can contain both class definitions and inline
scripting. When we compile or run a Groovy script, Groovy generates a class object
for each Groovy class that it encounters in the source. If the source file contains some
scripting elements, it also generates a class object for these.

Free download pdf