The DSLs in C sharp
A while back I wrote a blog post about as LINQ considered as a Domain-specific language. C# has DSLs, more of them over time, but as users of c# we don't have acess to this layer of the compiler.
A summary of the previous article is that LINQ is a whole of several parts. There are some enabling features that we as users can also use for our own purposes: type inference with "var", anonymous types, initializer blocks, lambdas and extension methods. Then there is a compiler rule that changes query syntax into the equivalent, e.g.
var longStringsLinq = from sItem in longStrings where sItem.Length > 5 select sItem.Length;
into
var longStringsLinq = someStrings
.Where(item => item.Length > 5)
.Select(sItem => sItem.Length);
This to me looked like a DSL. And we as users of the C# language cannot repurpose it. There are a couple of other DSL-style examples in C# that have the characteristics that they involve at least one special keyword which invokes a code re-writing rule that emits code that is simpler (but more verbose) c#, without the special keywords.
yield return is the first example, where the code is rewritten according to quite complex rules.
And the new CTP "async and await" feature recently announced for C# 5 is another example.
I keep thinking about ways and means of exposing this kind of rule as something that can be created not just to compiler-writers but to C# programmers.
No doubt making it work would be more complex that just supplying a BNF grammar and some rules, or an XSTL specification. Maybe it could be a C# class that transforms a branch of the parse tree. There may be tremendous potential to generate fragile, incorrect or poorly-performing code. But it also seems that creating your own code-generating DSL like this would be very interesting and powerful.