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

| by Srini Penchikala Follow 40 Followers on Oct 14, 2008. Estimated reading time: 3 minutes |

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 Stage

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.

Tell us what you think

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

Email me replies to any of my messages in this thread

Scala Notes by Daniel Spiewak

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).

Groovy for Runtime Errors by Ricky Clarkson

Groovy takes Java's existing syntax, dumps the static type checking and adds extra syntax. In Groovy, an explicit type causes a runtime assertion, making explicitly-typed code slower than untyped code.

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.

Re: Groovy for Runtime Errors by Srini Penchikala

Hi Daniel & Ricky, Thanks for pointing out about Scala and dynamic methods. I have updated the post to reflect this. -Srini

Re: Groovy for Runtime Errors by Vadim Mikhnevych

I feel sorry that InfoQ is not moderated and allows posts with links to "arap sikişi", like the one above. :-(

Re: Groovy for Runtime Errors by Floyd Marinescu

I feel sorry that InfoQ is not moderated and allows posts with links to "arap sikişi", like the one above. :-(

We have removed the spam you mentioned, we are doing our best to deal with these spammers, it's unfortunately a purely manual process to deal with.

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

5 Discuss