开发者

Is it possible to create C# language modifications as did LINQ?

I've been looking quite a bit at Mr. Sk开发者_JS百科eet's blog on how to re-implement LINQ.

In particular, he states that the code:

var list = (from person in people
        where person.FirstName.StartsWith("J")
        orderby person.Age
        select person.LastName)
       .ToList(); 

is translated to methods that are extension methods that are provided by the LINQ library:

people.Where(person => person.FirstName.StartsWith("J"))
  .OrderBy(person => person.Age)
  .Select(person => person.LastName) 

BY THE COMPILER.

My question is, how does one impress the bigwigs enough with a library to cause them to allow the language to change to support the library? Or were those words already reserved before LINQ came along?


Grab the Mono C# Compiler - it's open source and you can do whatever language modifications you want and which .net supports, e.g., use enums as generic constraints, create methods that return references to value types (public ref int Max(ref int x, ref int y) { if (x>y) return ref x; else return ref y; }), that have protected or internal visibility etc.

Of course, you are then creating an incompatible derivate of C#, but if you push it hard enough then people might like it.

The other option: Start a blog, come up with some really good use cases for it, possibly a sample implementation in a .net language or using a customized compiler, show what problem it solves and why this would be a big win that justifies the cost that goes into specifying, designing, developing, testing and documenting of the feature.


I highly doubt the compiler developers would implement syntax extensions for just any library. If you really wanted language-level integration, you could develop a pre-processor that transforms your custom syntax into valid C#. That's essentially what the compiler does with LINQ anyway (as you pointed out in your question).

Of course, you would lose things like auto-complete and syntax highlighting in Visual Studio, but that could be fixed with an extension.


Some languages allow to extend their syntax and semantics. The closest to C# is Nemerle (and it even supports a safe subset of C#, which you can, in turn, extend as you like), but the same can be done with almost any Lisp, for example. So, if you're using a language powerful enough, you don't need to "impress" anyone - any library can add new functionality to a language itself.

There were rumors that the next C# will provide some rudimentary metaprogramming support as well, but I could not find any specifics.


It is possible but it will be hard and they'll probably reimplement the idea to better fit their language constructs. The new async feature is similar to a library called AsyncEnumerator for example but they are building everything to better suit the language. The keywords for LINQ were not reserved in advance but they are contextual keywords meaning that there can be identifiers that match this keywords out of the LINQ context. When the compiler detects a LINQ construct it goes into LINQ mode where these keywords are actual reserved words.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜