Appendix E. A cheat sheet for Groovy’s DSL-friendly features – DSLs in Action

Appendix E. A cheat sheet for Groovy’s DSL-friendly features

This appendix will assist you as you become familiar with the DSL-friendly features of Groovy. Please don’t treat this information as a comprehensive language overview. For a more complete and detailed discussion of the language and its syntax, see the references in section E.2.

E.1. DSL-friendly features of Groovy

Groovy is a dynamically typed OO language with strong features of reflective and generative metaprogramming. Groovy shares the object model with Java and has strong interoperability with the Java language. Groovy can also be used as a scripting language. It has optional typing, operator overloading, strong literal syntax, and functional abstractions like closures. Table E.1 gives you a brief overview of the features of Groovy that make it a great language for designing DSLs.

Table E.1. Groovy feature overview
Class-based OOP
Groovy is OO. You can define classes that have instance variables and methods. The syntax is similar to Java and the default visibility modifier is public. For details about class definition syntax, refer to [1] in section E.2. class Account {
Integer balance(Date date) = {
//.. implementation
} This snippet shows a class declaration in Groovy.
Optional typing
You can declare static types just like you can in Java; these types are honored at runtime. Groovy also offers dynamic typing like Python does, where the type declaration is replaced by the def keyword. Formal parameters to method and closure declarations can even omit the def. String str = new String("Groovy");
str = 8
def dstr = "dynamic"
dstr = 20 str will be assigned the String 8 dstr will be assigned the Integer 20
You declare properties as fields with the default visibility modifier, no matter what type you’re using. class Foo {
String str
def dyn
} This snippet shows a class with properties.
You can define single-line strings, multiline strings, or a GString with placeholders. def single = 'single line string'
def multi = """ I am a multi line
def gstring = "$single has ${single.size}
characters" This code shows various strings that are supported in Groovy.
Collection data types
Groovy offers all common collection data types like Range, List, Map, and so on. All of them have a strong literal syntax that makes great DSL snippets. // range (half inclusive)
(0..<10).each { println it }
// list manipulation
[1,2,3] * 2 == [1,2,3,1,2,3]
[1,[2,3]].flatten() == [1,2,3]
[1,2,3].reverse() == [3,2,1]
[1,2,3].disjoint([4,5,6]) == true
// map definition literal syntax
def map = [a:0, b:1] Examples of various collection data types in Groovy.
A closure is a block of code that can be reified for later execution. It encapsulates some logic and an enclosing scope. def clos = { println "hello world!" }

clos() //prints "hello world!"

def mult = {x, y -> println x * y}

mult(2, 5) // prints 10 These snippets show closures in Groovy.
Builders help you build hierarchical data models with amazingly concise syntax. The secret sauce is metaprogramming. def builder = new
p("How are you?")
} Groovy builders work based on a combination of metaprogramming and closures.
The ExpandoMetaClass is one of the primary metaprogramming constructs that allows you to dynamically add methods, constructors, properties, and static methods using a closure syntax. Integer.metaClass.twice << {delegate * 2} This snippet adds a method named twice to the class Integer.This new method is visible to all threads in unlimited scope
This concept is similar to the ExpandoMetaClass, but visibility is limited to the scope that you explicitly specify. class IntegerCategory {
static Integer twice(Integer i) {
return i * 2
use (IntegerCategory) {
assert 4 == 2.twice()
} The method twice is visible only within the scope specified by use {}.

E.2. References

  1. König, Dierk, Paul King, Guillaume Laforge, and Jon Skeet, 2009. Groovy in Action, Second Edition. Manning Early Access Program Edition. Manning Publications.
  2. Subramaniam, Venkat. 2008. Programming Groovy: Dynamic Productivity for the Java Developer. The Pragmatic Bookshelf.