This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Scala and the fly likes DSLs - when? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "DSLs - when?" Watch "DSLs - when?" New topic
Author

DSLs - when?

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Picking up the DSL theme mentioned in another question....

I really like the idea of DSLs, especially embedded ones, and I've had some success on a small scale. Not what I'd call a full-scale DSL, but using fluent interfaces to make code much more readable. I've used them for unit test setup, and for configuration (I moved one application from a horrible XML configuration to what I think is a rather nice - and type safe! - fluent configuration in code). But nothing really serious.

So my question is about when it's worth developing a DSL. Are there any good guidelines about what sort of problem lend themselves to DSLs, and when it's worth the investment in developing a DSL? Any other general pieces of advice in this area?

Thanks.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2277
    
  28

Piggy backing off this question:- I would also like to add "DSL- Why Scala?". Is Scala much better than tools like Java CC or Antlr while implementing a DSL. Why would I use Scala over those tools?

I used Scala to implement a DSL parser that we needed. The reason I chose Scala was because I wanted to try it. The problem is that piece of Scala code is getting no adoption from the other developers because of Scala. I can get almost no love for my module, although everyone seems to think that it's very impressive. Makes me think that I should have just used JavaCC/Antlr to generate parser code.

Debasish Dg Ghosh
Author
Greenhorn

Joined: Jan 24, 2013
Posts: 4
[Disclaimer: I am the author of DSLs In Action]

A DSL makes your domain model more readable to the user. If you are writing code for a specific domain, readability of the code depends on how much close to the domain you can program. Let's get an example. Suppose I am writing a snippet of code for doing a fixed income trade on a stock exchange. For the snippet to be readable to the user/maintainer of the code, ti will be wonderful if I can write like the following :



This is concise and yet a sufficiently expressive DSL that models my intention. Now think of it - it's actually a valid snippet of Scala code from one of the example DSLs that I implement in the book DSLs in Action. Similar code can be developed in other languages as well like Ruby, Groovy, Clojure etc. The main idea behind a DSL is to be able to express your solution more in terms of the problem domain using the right language (Eric Evans calls this The Ubiquitous Language in his Domain Driven book).

Now for a more generic answer. When we design a domain model, the core model is a set of artifacts in a host language that abstracts the business. A DSL can be a thin linguistic abstraction that speaks the domain and builds on top of the core abstractions. I wrote a blog post on this quite some time back .. http://debasishg.blogspot.in/2010/02/dsl-grow-your-syntax-on-top-of-clean.html.

Thanks.

Matthew Brown wrote:Picking up the DSL theme mentioned in another question....

I really like the idea of DSLs, especially embedded ones, and I've had some success on a small scale. Not what I'd call a full-scale DSL, but using fluent interfaces to make code much more readable. I've used them for unit test setup, and for configuration (I moved one application from a horrible XML configuration to what I think is a rather nice - and type safe! - fluent configuration in code). But nothing really serious.

So my question is about when it's worth developing a DSL. Are there any good guidelines about what sort of problem lend themselves to DSLs, and when it's worth the investment in developing a DSL? Any other general pieces of advice in this area?

Thanks.
Debasish Dg Ghosh
Author
Greenhorn

Joined: Jan 24, 2013
Posts: 4
Let me try to answer : Why Scala and not Java CC or Antlr ..

It's not the question of why not a specific technology. My response is that it's always good to have options. And the options here are quite different and useful in different use cases. When we design a DSL using Scala, we use the power of the host language to embed the DSL within Scala. This is typically called "internal" DSL or "embedded" DSL. The advantage is that you don't have to design scanners and parsers ground up and instead use the power of the host language to do these things. You also get to use the type system and the exception handling capabilities of the language when you implement an internal DSL.

So theoretically you can design an embedded DSL in any language you choose. But the more powerful abstractions the language offers, the more succinct syntax the language offers, the more power you can get while designing your DSL. In the book DSLs. In Action, I highlight some of the specific features that make Scala a good host language to embed your DSL. They are :
  • flexible syntax
  • extensible object system
  • functional programming capabilities
  • statically checked duck typing
  • lexically scoped open closures
  • implicit parameters
  • modular composition


  • I discuss each of these features along with real life examples in the book.

    Having said that ANTLR like tools is also a very powerful way to design your DSL. These are cases when you would like to have more control over the language than what a host language offers. You need to design your own parsers for your language. These are called "external DSL". The book also discusses in detail how you can design external DSLs using ANTLR and integrate with workbenches like Eclipse XText.

    Using Scala also you can design external DSLs using the power of parser combinators. These abstractions give you composable artifacts which you can bind together and build your own parser for your own language, and all these without getting out of the host language model. I have a separate chapter in the book that discusses parser combinators in Scala.

    Hope this clears your doubts.

    Thanks.


    Jayesh A Lalwani wrote:Piggy backing off this question:- I would also like to add "DSL- Why Scala?". Is Scala much better than tools like Java CC or Antlr while implementing a DSL. Why would I use Scala over those tools?

    I used Scala to implement a DSL parser that we needed. The reason I chose Scala was because I wanted to try it. The problem is that piece of Scala code is getting no adoption from the other developers because of Scala. I can get almost no love for my module, although everyone seems to think that it's very impressive. Makes me think that I should have just used JavaCC/Antlr to generate parser code.

    Jayesh A Lalwani
    Bartender

    Joined: Jan 17, 2008
    Posts: 2277
        
      28

    Thanks Debashish. Internal DSLs sounds very interesting. I implemented my parser using combinators, which probably led to the parser being more complicated than it should be. I think I feel into the pitfall of using Scala like Java, even though there are better ways of doing things.
    Debasish Dg Ghosh
    Author
    Greenhorn

    Joined: Jan 24, 2013
    Posts: 4
    Internal DSLs are indeed interesting and more popular than the external DSLs. And more the power of abstraction that your host language offers, more expressive can your DSLs be. I have been working with internal DSLs in Scala for quite some time now and I find Scala very approachable in that respect.

    Jayesh A Lalwani wrote:Thanks Debashish. Internal DSLs sounds very interesting. I implemented my parser using combinators, which probably led to the parser being more complicated than it should be. I think I feel into the pitfall of using Scala like Java, even though there are better ways of doing things.
    James Volker
    Greenhorn

    Joined: Feb 08, 2013
    Posts: 3
    I have been using Spring Integration for a while, and recently looked at thier Scala DSL implementation. It makes the configuration a lot easier to read. You can read about the WHY they added a DSL at this at this blog post. For an interesting discussion of the difference between a using a Groovy DSL and the Scala DSL, read this post.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: DSLs - when?
     
    Similar Threads
    Multi File Templates in Eclipse (Code Generation?)
    DSL's written in Ruby - a Ruby in Practice query
    Why DSLs? What will we get out of your book?
    Dynamic Language Support in Java 7
    Dumbing it down: How or when to let users interact with DSL?