开发者

DSLs vs. Plain Old Libraries

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 integr开发者_Python百科ate 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜