Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • A command-expression is composed of an even number of elements
  • The elements are alternating a method name, and its parameters (can be named and non-named parameters)
  • A parameter element can be any kind of expression (ie. a method call foo(), foo{}, or some expression like x+y)
  • All those pairs of method name and parameters are actually chained method calls (ie. send "hello" to "Guillaume" is two methods chained one after the other as send("hello").to("Guillaume"))

Interesting benefit of the enhanced command expressions

More and more do we see Java Fluent APIs that chain method calls, returning this, so as to "build" a new object.
For instance, you can imagine a fluent API for building an Email message, that would look something like this in Java:

Code Block

Email.from("foo@example.com").to("bar@example.com").subject("hello").body("how are you?")

In Groovy, with the extended command expressions, this could become:

Code Block

Email.from "foo@example.com" to "bar@example.com" subject "hello" body "how are you?"

Notice the absence of parentheses and dots.

Example: A DSL for SQL

No Format
SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...)

...