BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Is Groovy a Better Choice Than Java for Creating Internal DSLs?

Is Groovy a Better Choice Than Java for Creating Internal DSLs?

This item in japanese

Bookmarks

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.

Groovy's use in writing DSL's has been well documented and there are several DSL implementations like behavior testing DSL, Groovy based Behavior Driven Development (BDD) tools GSpec and easyb, and Groovy Finite State Machine (FSM) DSL .

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.

Rate this Article

Adoption
Style

BT