views:

30

answers:

1

Is there a way to add custom linq keywords and tell the compiler how to translate them to actual extension methods?

For example, translate the single keyword:

var color = from c in colors
            where c.IsFavorite
            select single c

To

var color = colors.Where( c => c.IsFavorite ).SingleOrDefault();
+2  A: 

No there is not a way to do this.

As to why, I worked on the VB.Net LINQ implementation vs. C# but the issues are mostly the same.

Adding LINQ to the language was a huge undertaking. As Eric Lippert has blogged about recently, LINQ barely fit into the VS2008 schedule and as such, essentially only the features that were absolutely essentially to shipping LINQ were added to the language.

Making LINQ arbitrarily extensible to users was not one of those features. It's also something that would have been very costly. Right now LINQ is a very complex feature which has a fixed set of constructs. Allowing it to be arbitrarily extensible would have severely inflated these costs (especially on the IDE side) in at least the following areas

  • Language Design (huge)
  • Intellisense
  • Pretty Printing / Formatting
  • Low level code emit details
  • etc ...
JaredPar
:) Short and simple answer I guess. Any more specific info on why not? Is cause the compiler is closed source, a specific limitation in the C# standards etc?
Paul Alexander
@Paul, added a brief explanation as to why.
JaredPar
Thanks for the great feedback!
Paul Alexander