Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Groovy is particularly well suited for writing a DSL: Domain-Specific Language. A DSL is a mini-language aiming at representing constructs for a given domain. Groovy provides various features to let you easily embed DSLs in your Groovy code:

  • the builder concept lets you write tree structured languages
  • you can add new methods and properties on arbitrary classes through categories or custom metaclasses, even numbers: 3.euros, 5.days, etc
  • most operators can be overloaded: 5.days + 6.hours, myAccount += 400.euros
  • passing maps to methods makes your code look like methods have named parameters: move( x: 500.meters, y: 1.kilometer )
  • you can also create your own control structures by passing closures as the last argument of a method call: ifOnce( condition )
    { ... }; inTransaction { ... }
  • it is also possible to add dynamic methods or properties (methods or properties which don't really exist but that can be intercepted and acted upon) by implementing GroovyObject or creating a custom MetaClass
    Guillaume Laforge gave some thoughts and examples on that topic on his blog. John Wilson has implemented a DSL in his Google Data Support to make operations on dates easier.
  • No labels