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...