Book Image

Groovy for Domain-Specific Languages, Second Edition

By : Fergal Dearle
Book Image

Groovy for Domain-Specific Languages, Second Edition

By: Fergal Dearle

Overview of this book

Table of Contents (20 chapters)
Groovy for Domain-specific Languages Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introduction to DSLs and Groovy
Index

Groovy bindings


Every Groovy script has an associated binding object. The binding is where instances of variables referenced within the script are stored. The binding is an instance of the class groovy.lang.Binding, and we can access it in any script by referencing the built-in variable binding, as the next example will show.

Note

When we reference a previously undeclared variable in a script, Groovy creates an instance of the variable in the binding. On the other hand, variables that are defined within the script are considered local variables and are not found in the binding. The latter provides a convenient placeholder where Groovy can store these variables. This also presents the DSL with an opportunity. By manifesting variables in the binding, we can manipulate the script with predefined values. By adding a closure to the binding, we can provide built-in methods for the DSL.

In the following example, when we reference a new variable named count in a script, we see how that variable is...