views:

69

answers:

2

I've been thinking lately about the question of DSLs vs. libraries. In my field, the problem with DSLs (R, SAS and Matlab come to mind) is that they're a PITA to write more general purpose code in and a PITA to integrate into more general-purpose code in other languages. I'm not saying either is impossible, just annoying and frustrating.

This contrasts with the approach of, for example, NumPy, which runs on top of a general-purpose language and would probably be as good as Matlab if it had existed for as long and had as much money poured into it. This allows you to work on a project where only a little bit is numerics without needing a whole bunch of crufty glue code to interface between languages, having to remember multiple syntaxes, etc.

What are the advantages of a standalone DSL over a plain old library in a general purpose language? It seems like there are such obvious disadvantages in terms of more difficult integration with more general-purpose code and yet another syntax to learn that I just can't understand why these DSLs are so popular.

+1  A: 

One of the main advantages of a standalone DSL is that it can be more expressive. In a library written in the general purpose language, you are limited by the syntax of the language. Consider an example of banking software that has some financial records that can be queried and you want to find all mortgages issues in the last year. Pseudocode for a query in a general purpose language would look something like:

List<Mortgage> mortgagesThisYear = new LinkedList<Mortgage>();
for ( Mortgage m : mortgages )  {
  if ( today.minus(m.getDate()) <= 1 year ) {
    mortgagesThisYear.add(m);
  }
}

Now in a DSL, you might be able to write something like:

Find all mortgages in the last year

More so, bankers could construct these queries based on a "ubiquitous language." (a language of the domain that is common and that makes it easier for programmers and bankers to communicate).

Stand along DSLs are meant to very specific and not general purpose. The language is more expressive and the syntax is generally more relaxed and more related to the domain.

Jeff Storey
ok, but let's say you had a higher level general purpose language. Then it might be:lastYear = [m for m in mortgages if today() - m.date < 365]
dsimcha
I think the same ideas still apply. The DSL is all about an expressive language in the domain in which you're working. It is meant to be specific and easier to read/use than a higher level language.
Jeff Storey
A: 

The disadvantage of external DSLs doesn't apply for external DSLs implemented using code generation and generation gap pattern. Interfacing is then made through the one general purpose language used. Actually, internal DSLs have many more disadvantages, like no good readable explicit structure, bad practice coding, limited concrete syntax possibilities, no tools support, very hard or impossible to implement domain specific validations and interfacing problem with other code, when the DSL implementation is too criptic.

The0retico