Registered: Mar, 2006
Re: How and When to Develop Domain-Specific Languages?
Posted: Mar 10, 2006 9:02 AM
I have tried Rails, and I don't know if it is a DSL. DSL means domain-specific-language. So that's a language. If you're used to Java, C#, or another static language it probably looks like a DSL. If you're used to python, it will look more like a group of classes and functions, with some other weird things. If you're used to Ruby, you'll know that this is not a DSL, but just a library like any other. In TCL, Lisp, and many other languages you can create APIs that look even more like a separate language.
For example, the Ruby-Make "DSL" in a C#/Java Like language:
public class ATask : Task
public void execute()
public static void Main()
task :a_task do
The first example doesn't look like a DSL because you can clearly see this is not a new language. There are too many aspects (such as a class, a main function, etc) that are not domain specific. The most common way to handle this is to create an xml "dsl", or configuration. A static language tries to be a dynamic one ;-).
The ruby version looks more like a domain specific language, but there are some things a Ruby programmer would recognize: the "do" and "end" keywords, and the syntax to create a symbol: ":a_task".
In the Lisp code, there are no keywords. The domain specific language is truly domain specific. The only weird things are the (()). But unfortunately, the parens make it possible to create a DSL.
class Posts < ApplicationController
This is more domain specific, because it contains no language keywords (def, class, end).
Or maybe a model from django:
author = meta.ForeignKey(Author)
title = meta.CharField()
content = meta.TextField()
So what makes an api a DSL? This is not entirely clear, because most DSLs will have some non DS aspects, and you can't say: "if an api contains less than n non DS aspects, it is a DSL".
I think most people will agree that my first Java/C# like examle is not a DSL. But is the Django model a DSL?
DSLs are about abstractions. You can create better DSLs in languages with better ways to abstract. Many languages support procedural abstrations (FORTRAN, Python, Ruby, Lisp). A lot support object oriented abstractions (Python, Ruby, Lisp). A few support functional abstractions (Ruby, Lisp). Even less languages support syntactical abstrations (Lisp).
(note that because these languages are turing equivalent, you could write a lisp interpreter in FORTRAN, and have all these abstractions).
So "How and When to Develop Domain-Specific Languages?"?
By choosing a language that supports powerful abstractions.
Just create an appropriate abstraction for difficult things.
If you notice that you want to generate a form, and you're doing it this way:
<input type="text" name="title" />
<input type="submit" value="save" />
Create an abstraction:
Or (in Java/C#):
<?xml version="1.0" ?>
<text-field name="title" />
<text-area name="content" />
<submit-button caption="save" />
and in your code:
and in your code:
To make it easier next time.