Groovy DSL
Groovy lets us omit parentheses around the arguments of a method call for top-level statements. "Command chain" feature extends this by allowing us to chain such parentheses-free method calls, requiring neither parentheses around arguments, nor dots between the chained calls. The general idea is that a call like:
a b c d
will actually be equivalent to:
a(b).c(d)
This also works with multiple arguments, closure arguments, and even named arguments. Furthermore, such command chains can also appear on the right-hand side of assignments.
Also see [dsl_wiki].
Here's a small primitive real example:
class Box {
private int content = 0
Box(int content) { this.content = content }
Box add(int value) { content += value; return this }
void show() { println(content) }
}
static Box box(int content) { return new Box(content) }
box(5).add(3).add(2).show() // 10
The last line can be rewritten the following way:
box 5 add 3 add 2 show() // 10
The last component show()
is not clean enough as it has braces. In this situation it's
unavoidable.
There are various strategies that we can use. The following example will use maps and closures:
show = { println it }
square_root = { Math.sqrt(it) }
static def please(action) {
[the: { what ->
[of: { n -> action(what(n)) }]
}]
}
please(show).the(square_root).of(121)
// 11.0
The equivalent form:
please show the square_root of 121
// 11.0