BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Building Domain-Specific Languages in JRuby

Building Domain-Specific Languages in JRuby

Bookmarks
Closing out the Java One conference last week was Rob Harrop's presentation "Exploiting JRuby: Building Domain-Specific Languages for the Java Virtual Machine." Domain specific languages (DSLs) have been gaining popularity, as shown on InfoQ with a presentation on an introduction to domain specific languages by Martin Fowler and posts on the debates in the blogsphere, and provide a way to create a custom language for a specific programming or business purpose.

But why would you want to do this? There are many possible answers, some of these are: repetitive tasks can be simplified; boiler-plate code can be encapsulated; or an API can be better express the intent of the code. No matter what your particular reason, all DSLs should deliver a clear value to your project.

The approach explored in the presentation was to create a JRuby DSL that is utilized from within Java classes. As byte code generated from JRuby will run in the JRE, the DSL is categorized as "internal" - which uses a general programming language to create a specific programming language. The alternative is an "external" DSL that requires an external compiler and generator, making the integration much more difficult.

The benefit in this approach is in creating the DSL, and several JRuby language techniques were presented to make developing DSLs easier:

  • Operator overloading - overloading operator allows the DSL to provide a natural syntax
  • Hashes and symbols - using hashes and symbols make it easy to express relationships and identify objects
  • Blocks - blocks encapsulate executable logic, and allow the logic to be stored (in hashes) for later execution
  • Dynamic type extension - methods can be added dynamically to classes and objects
  • Method missing - when a method is called that doesn't exists it can be trapped, avoiding the need to know all the operations in advance
  • Integration - take advantage to existing Java code

To take full advantage of using JRuby in creating the DSL, tips on style were provided:

  • Ensure that you are not writing Java code in a dynamic language
  • Build up the DSL from common building blocks
  • Identify the problem and then create a syntax to express the solution
  • Provide metadata and behavior - don't let DSLs become configuration files
  • Think the ruby way: use type extensions; use blocks; use methods on objects
  • Keep the scope limited; address a small part of your domain and don't try to make the DSL a general programming language

For developers, DSLs are another tool that can be utilized. By investing a little time upfront, large time savings can be achieved.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Groovy, an other option

    by Guillaume Laforge,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Additionally, there's the option to use Groovy for embedding DSLs in your Java EE application. There's a short page on the Groovy site with a couple pointers to how to create Domain-Specific language in Groovy -- especially the tutorial from QCon 2007 which provides some explanations on the various tricks that Groovy offers.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT