blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Anthony Steele's Blog

LINQ as a DSL

What kind of thing is Language Integrated Query, exactly? You can draw the line in a number of places, but I am going to cut it down to the core, where LINQ is a Domain Specific Language.

 

There are LINQ-enabling language features, i.e the language extensions of type inference:

            var s = "This is a string";

 

Anonymous types and instance initialiser blocks:

            var anObject = new { x = 3, y = 4.5, z = true };

 

Lambdas:

            var someStrings = new List<string>();

            var longStrings = someStrings.Where(item => item.Length > 5);

 

Also extension methods, and the c# code in System.Linq.

 

So, once all of this is factored out, what is left of LINQ? It is just a compiler rule (or rules) for turning

            var longStringsLinq =

                from sItem in longStrings

                where sItem.Length > 5

                select sItem;

 into

            var longStringsLinq = someStrings.Where(item => item.Length > 5).Select(sItem => sItem);

 

Whereafter compilation carries on as before. The transformation rule is a Domain-Specific Language which is baked into the c# Version 3 compiler.

 

We have the option of using the other enabling features for our own purposes. But the LINQ DSL is not accessible, unlike in some languages such as Ruby, in which DSLs are user accessible. I still prefer reading c# code to Ruby code, but I'm thinking - what would c# look like if we could define our own DSLs in a relatively easy way? What would you do with it? Would chaos or enlightenment result?

 

(this post was inspired by Neal Ford's talk at devweek on "“Design patterns” in dynamic languages")

Published 14 March 2008 19:14 by Anthony.Steele
Filed under: , , , , ,

Comments

No Comments
Anonymous comments are disabled

About Anthony.Steele

Programmer in c# for Conchango
Powered by Community Server (Personal Edition), by Telligent Systems