Geertjan's Blog

  • October 30, 2007

Groovy DSL

Geertjan Wielenga
Product Manager
InfoQ has a great (i.e., entertaining, interesting, and pragmatic) interview with two Domain Specific Language (DSL) developers, here. I highly recommend watching it. In that interview, Ruby is used to define the DSL, but Groovy can also be used for this purpose, for which all the related resources can be found here. But, I wanted to really understand what all of this is about, and even though the sessions on this subject at the Grails eXchange were helpful, it wasn't until I followed the steps in this blog that I began to really "get it". At the end of it, you can calculate distances and measurements and compare them to each other, using a brand new notation defined in Groovy DSL. So, as one can see in the editor below, it is possible to use the DSL to type '1000.m.km' and, because of your DSL, Groovy is able to understand that what you are actually saying is: "Convert 1000 meters to kilometers", such that it correctly resolves it, as shown in the Output window below:

Note that, in Groovy, "1000.m.km" doesn't mean anything at all. In fact, the Groovy file would have thrown a runtime error, if I had typed that string without specifying the DSL that I wanted to use to interpret my string.

All this is pretty cool to see in action. Basically, what you're doing is extending Groovy. Now it makes real sense to me and now I can begin reading the theoretical stories and the slides from various presentations. Having a complete, working sample is the beginning of knowledge.

Join the discussion

Comments ( 3 )
  • Tom Wheeler Tuesday, October 30, 2007

    BTW, my colleague Mario Aquino wrote a Java News Brief article about Domain Specific Languages using JRuby earlier this year: http://www.ociweb.com/jnb/jnbJan2007.html

  • Olivier Wednesday, October 31, 2007

    This is not DSL, this is API!

  • Geertjan Wednesday, October 31, 2007

    Olivier, this is what Groovy refers to as "an embedded DSL", as far as I am aware. Tom, that's cool, I learned a lot from that reference, thanks!

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.