Java Mailing List Archive

http://www.gg3721.com/

Home » user.groovy »

[groovy-user] [Groovy 1.8] Nicer DSLs with extended command expressions

Guillaume Laforge

2010-08-30

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi all,

A quick heads-up to tell you about an upcoming Groovy 1.8 feature
which will allow us to make nicer DSLs.

Lidia Donajczyk was our Google Summer of Code student this year,
working on the implementation of GEP-3, an extension to Groovy's
command expressions.
You can have a look at this page for the guiding ideas behind this
enhancement proposal:
http://docs.codehaus.org/display/GroovyJSR/GEP+3+-+Command+Expression+based+DSL

Just like you could use simple command expressions before, like:

println "hello"
move left
make coffee
drink tea

GEP-3 extends this for more complex sentences such as:

move left by 30.centimeters
sendFrom "Guillaume" to "Jochen"
send from: "Jochen" to "Lidia"
sell 100.shares of MSFT
take 2.pills of chloroquinine in 6.hours
blend red, green of acrylic
wait 2.seconds and execute { assert true }

A more thourough example is this:

Recipe.instructions {
  take medium_bowl
  combine soy_sauce, vinegar, chili_powder, garlic
  place chicken into sauce
  turn once to coat
  marinate 30.minutes at room_temperature
}

(full implementation here
http://groovyconsole.appspot.com/script/214001 or in Groovy's test
cases under gls.syntax.Gep3Test.groovy)

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, and a mix of them)
- 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"))

Current limitations:
- Extended command expressions are not yet allowed on the RHS
(righ-hand side) of assignments, so you can't yet do: def movement =
move left by 30.centimeters
- Also, we can't yet use methods with zero-arguments.

Hopefully, we'll work on those limitations before the final release of
Groovy 1.8.
One of the nice aspects of this extended command expression pattern is
that it should play nicely with most Java APIs following the Java
builder pattern.

--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.