11

I've been looking quite a bit at Mr. Skeet'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?

user420667
  • 6,552
  • 15
  • 51
  • 83
  • 7
    become Jon Skeet? – Dustin Davis Aug 30 '11 at 17:31
  • 3
    Microsoft "big wigs" *love* cookies.... – FishBasketGordo Aug 30 '11 at 17:32
  • 7
    You realize LINQ was developed by Microsoft, the same company that dictated C#, .NET and the CLR, right? – BoltClock Aug 30 '11 at 17:32
  • 6
    I'm not sure why this was closed. Obviously OP won't be able to convince the compiler devs to implement new syntax for his or her library, but this question can easily be answered with "write a pre-processor that runs before the compiler." – David Brown Aug 30 '11 at 17:34
  • @BoltClock: I suspected as much but it seemed odd that you would have to include the library if it was part of the language to begin with. – user420667 Aug 30 '11 at 17:39
  • @David Brown: I like that idea. – user420667 Aug 30 '11 at 17:40
  • @user420667 There is nothing odd that in Microsoft different projects are compiled in different dll's. .NET FCL contains many dll. – VMAtm Aug 30 '11 at 19:00
  • 1
    See http://stackoverflow.com/questions/5780648/has-the-c-spec-team-committee-ever-considered-this-object-creation-syntax for the canonical "why not add syntax" answer. – John Saunders Aug 30 '11 at 19:43
  • @VMAtm: Yes, but you don't expect to have to import a library to be able to use access modifiers or other parts that you treat as the language. I'm not saying it's at all that it's a bad design choice, it just struck me as odd. – user420667 Aug 30 '11 at 21:29
  • 1
    @user420667 You don't have to import the library for the syntax. You have to import the library to use the syntax *with `IEnumerable`*. You could write classes that provide the methods to which the special syntax compiles. – Gideon Engelberth Aug 30 '11 at 22:04

4 Answers4

9

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.

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • You can't do what every you want by changing the compiler. Since it just turns C# into CLR, you are still restricted to the rules of CLR. You'll need to modify the .NET framework itself *and* the compiler. – Stefan Steinegger Aug 30 '11 at 19:44
  • @Stefan, you can “fake” a lot of things CLR doesn't support. Either by implementing them yourself (like C# closures) or by pretending they do exist, but don't include them in the generated code (like F# units of measure or Java generics). – svick Aug 30 '11 at 19:59
  • @Stefan: That's what I said - " modifications you want and which .net supports". So if you want stuff like "Deterministic Finalization", you're out of luck of course. But there are tons of features that .net supports that C# doesn't. – Michael Stum Aug 30 '11 at 20:01
  • @Michael: ok, sorry, didn't notice this. – Stefan Steinegger Aug 31 '11 at 09:36
3

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.

David Brown
  • 35,411
  • 11
  • 83
  • 132
1

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.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
0

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.

Stilgar
  • 22,354
  • 14
  • 64
  • 101