Versions Compared

Key

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

...

Number:

GEP-3

Title:

Command Expression based DSL

Version:

1

Type:

Feature

Target:

1.7 8 or 2.0

Status:

Draft

Leader:

Jochen "blackdrag" Theodorou

Created:

2009-06-30 -06

Last modification:

2009-3012-06 11

Last update by:

Guillaume Laforge

Abstract

Since Groovy 1.0 Groovy supports command expressions. These are method calls without parenthesizing the arguments. This would be in theory a nice base for DSLs, but our command expressions are too limited, because we were not able to find easy rules on how to handle multiple arguments. This proposal now tries to close the gap by defining the evaluation order and meaning of those arguments. The concept is very near to what Scala allows, but is not equal for historic reasons.

...

  • existing valid usages must be kept as much as possible (for obvious backwards compatibility reasons)
  • the evaluation must be easily explainable
  • the grammar should support it

...

expression

possible meanings

allowed in old syntax

foo {c}

foo({c})

(tick) (same meaning)

foo a1

foo(a1)

(tick) (same meaning)

foo a1()

foo(a1())

(tick) (same meaning)

foo a1 {c}

foo(a1({c}))

(tick) (same meaning)

foo a1 a2

(error)

(error)

foo a1() a2

(error)

(error)

foo a1 a2()

(error)

(error)

foo a1 a2 {c}

(error)

(error)

foo a1 {c} a2

(error)

(error)

foo a1 {c} a2 {c}

(error)

(error)

foo a1 a2 a3

foo(a1).a2(a3)

(error)

foo a1() a2 a3()

foo(a1()).a2(a3())

(error)

foo a1 a2() a3

(error)

(error)

foo a1 a2 a3 {c}

foo(a1).a2(a3({c}))

(error)

foo a1 a2 a3 a4

(error)

(error)

foo a1 a2 a3 a4 {c}

(error)

(error)

foo a1 a2 a3 a4 a5

foo(a1).a2(a3).a4(a5)

(error)

foo a1() a2 a3() a4 a5()

foo(a1()).a2(a3()).a4(a5())

(error)

foo a1 a2 a3 a4 a5 {c}

foo(a1).a2(a3).a4(a5({c})

(error)

I think that The table shows enough to recognize the pattern. The attached block has a special role as it does not count as argument on its own directly. Instead the block is always bound to the identifier before and makes a method call. that itself is no command expression, but a normal method call expression. As can be seen too, this syntax nicely extends the existing Groovy syntax. Of course this also means, it will not be possible to omit commas if multiple arguments are used. A case that is not supported today anyway. For a DSL that is not really a problem though.

Summary of the pattern

  • 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"))

Example: A DSL for SQL

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

...

Code Block
Java
Java
sql.select(sql.count("column_name"), from:"table_name"

More example ideas

Here are some additional examples which relate to various domains, which may make the idea more visual in our minds.
These examples also mix named and non-named arguments, the use closures or not.
In comments, alongside the example, you'll see the equivalent non-command expression interpretation.

Code Block

sell 100.shares of MSFT // sell(100.shares).of(MSFT)
given { ... } when { ... } then { ... } // given({}).when({}).then({})
every 10.minutes, execute {} // already possible with current command expressions
schedule executionOf { ... } every 10.minutes // scheduler(executionOf({})).every(10.minutes)
blend red, green of acrylic // blend(red, gree).of(acrylic)

// named parameters into the mix
select from: users where age > 32 and sex == 'male'
// equivalent to select(from: users).where(age > 32).and(sex == 'male')
// not that however for this example, it would be intersting 
// to transparently convert the boolean conditions into closure expressions!

// a recipe DSL
take mediumBowl
combine soySauce, vinegar, chiliPowder, garlic
place chicken in sauce
turn once to coat
marinate 30.minutes at roomTemperature

Extension to command expressions in the case of assignments

Currently, command expressions are allowed as standalone top-leval statements or expressions, but you can't assign such an expression to a variable with keeping that nice DSL syntax. For instance, while you can do:

Code Block

move left

If you wanted to assign that command (which could return a Position instance), you would like to do

Code Block

def newPosition = move left

But you still have to do

Code Block

def newPosition = move(left)

So the GEP-3 proposal also suggests we extend command expressions to be allowed on the RHS of assignments.

Code Block


h2. Differences to Scala

...



For historic reasons

...

 {noformat
}println foo{noformat}has to be supported. This seems to not to be a valid version in Scala, since that would be interpreted as

...

{code
:Java
Java
}println.foo

and not as

Code Block
Java
Java
this.println foo

...