JVM-compatible languages such as Scala, Groovy and JRuby are recently gaining more popularity for developing Domain Specific Languages (DSLs). But are they better suited to creating internal DSLs than the Java programming language? Venkat Subramaniam explains why "Essence over ceremony" and "Metaprogramming" features in a dynamic language like Groovy help in developing internal DSLs.

He suggests that Java language is a better fit for creating external DSLs than internal ones, which are more dependent on the host language syntax. But when it comes to internal DSLs, the power of dynamic methods in Groovy language is a better fit. Venkat recently wrote about the metaprogramming feature in Groovy and how to take the advantage of this technique to writing internal DSLs.

Metaprogramming, which is a common characteristic of newer Java-platform languages like Groovy and JRuby but not of the Java language, helps in building internal DSLs allowing the developers to add and invoke methods dynamically. Groovy maintains a metaclass for each Java class. The developers can easily add methods to classes at runtime. It also makes it easy to synthesize methods at runtime by using methodMissing method.

Venkat explained the steps involved in creating a Groovy-based internal DSL with a sample application which is based on a simple input file. He also compared processing the example DSL from within Groovy code and from within the Java code with a sample application.

InfoQ spoke with Venkat about the current usage of DSL's (external and internal) in enterprise applications and the future role of the dynamic languages in creating DSLs. He said:

DSLs have been in popular use for a long time in enterprise applications. What is new probably is an increased awareness of these and more tools availability. Certainly the momentum behind dynamic languages is helping with the use of internal DSLs.

Metaprogramming in Groovy seems to be a powerful feature. What are the typical use cases for using this feature in developing web applications?

It certainly is. GORM already uses this quite extensively in Grails for Web applications. If you add a field to a table, you can readily use finder methods on the corresponding model class to query for objects based on the values of this newly added field. These methods are synthesized dynamically based on convention instead of you having to create these methods manually. You can also take advantage of Metaprogramming anywhere you need similar dynamic behavior. You can also use it anywhere you'd have considered using AOP techniques.

How do you see the future role of dynamic languages in creating the internal DSL's?

There are certainly some challenges. On one hand, dynamic languages make it insanely easy to parse and process internal DSLs. However, validating the internal DSLs is not easy. I am sure tool support as well as good validators will emerge in the coming years. This is certainly an area of good interest for research and experimentation. Assuming a few of these hurdles are crossed relatively soon, the ability to mix dynamic languages with more traditional languages on the same platform like Java and .NET will make internal DSLs highly preferable.

In a previous article, Venkat explained, using a sample application, how to create external and internal DSLs using Java language. A more detailed discussion of Metaprogramming in Groovy can be found in this book by Venkat. For more information on the role of Java in creating internal DSL, check out this article. More InfoQ coverage on DSLs can be found here.

Methods are not dynamic in Scala. It may *look* like they are, but the language is purely static and everything is well-typed. Just because a language provides the facility to add methods to existing classes does not make it dynamic (or non-lexically scoped).

In other words, when you use Groovy, you're making a conscious decision to push errors to the runtime, or you don't know what that means. So Groovy is appropriate for programs where runtime errors don't matter much (for this reason I don't mind configuring my text editor in elisp) or for novices.

It's kind of worrying that infoq allows someone to write an article suggesting that Scala has dynamic methods. It doesn't. Scala's mechanism for adding methods to classes is closer to (but better than) C#'s extension methods than it is to Ruby's open classes.

Is your profile up-to-date? Please take a moment to review and update.

Email Address

Note: If updating/changing your email, a validation request will be sent

Company name:

Keep current company name

Update Company name to:

Company role:

Keep current company role

Update company role to:

Company size:

Keep current company Size

Update company size to:

Country/Zone:

Keep current country/zone

Update country/zone to:

State/Province/Region:

Keep current state/province/region

Update state/province/region to:

Subscribe to our newsletter?

Subscribe to our architect newsletter?

Subscribe to our industry email notices?

You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.