C#5 and Meta-Programming

Share/Bookmark

We’ve been extremely busy the last months in order to have a well
polished release of NDepend v3 completely integrated in VS 2010, 2008
and 2005.

Now, having NDepend as a companion in all my VS instances makes each
of my programming day bright [:)]. But it is already time to think about
what’s next. We have tons of plans and you are welcome to publish all
your secret wishes for NDepend vNext in the comment of this post.

At least, now I have some time to look what is happening outside of
the NDepend sphere. I stumbled on this great talk:
C# 4.0 and beyond by Anders Hejlsberg
and I foster you to watch it,
at least from the minute 55 if you are already acquainted with C#4
features. Anders explains that C# and VB.NET compilers are currently
being migrated from C++ to managed code. A goal is to achieve Compiler
as Service
paradigm.Compiler as Service will let the programmers
evaluate on the fly a piece of code from a string, to an in-memory live
IL code. Concretely, it is as easy as calling a CSharpEvaluator.Eval(String):
Delegate
method. The input argument string contains a C# method
declaration and body. The output is a delegate object pointing to the
freshly compiled method. The method can then be executed at whim by
invoking the delegate object. Several pieces of code can be evaluated
consecutively in order to generate living code at runtime.

As far as I understand, the primary motivation behind more flexible
compilers, is to provide a framework that makes easy to write code that
can generate new code and refactors existing code! This, is the door
open to what is called meta-programming,
definition from wikipedia:

Metaprogramming
is the writing of computer programs that write or manipulate other
programs (or themselves) as their data, or that do part of the work at
compile time that would otherwise be done at runtime. In many cases,
this allows programmers to get more done in the same amount of time as
they would take to write all the code manually, or it gives programs
greater flexibility to efficiently handle new situations without
recompilation.

To summarize:

  • C#1 was all about delivering a new language for a new platform.
  • C#2 was all about providing generics to improve strong typing,
    especially in collection handling situations.
  • C#3 was all about letting write strongly typed queries abstracted
    from the date source. As a consequence C#3 fosters a more declarative
    way of programming.
  • C#4 was mainly about dynamic programming to inter-operate with
    dynamic programming environment.
  • C#5 will be concerned with meta-programming.

(On a personal recurrent note, I still consider C# as being
flawed since it doesn’t support non-nullable
reference types
. This might look like a detail, but this single
flaw hinders each of my programming day for 8 years now, that I am
programming with C#. I can attest that the absence of non-nullable
reference type is more or less responsible for a third of bugs my team
is facing).

If I understand well, C#5 will open the possibility to refactor
automatically a portion of source code (at compile time or at runtime?),
in order to make it runnable concurrently on multi-core. Making
possible to write and maintain concurrent code more easily will be a
concern of C#5. But I guess concurrency is just one domain of
application of future C#5 language and compiler enhancements (the same
way integrated queries are just one domain of application of C#3
language enhancements). We are living exciting times [:)]

Finally, if you cannot wait putting your hands in C#5 eval things and
imitating Anders Hejlsberg doing magic, know you can play with the Mono
compiler that already supports the Compiler as Service paradigm (see
this Miguel’s post).
Notice that for once in the Mono sphere, a special care has been taken
to make this Mono C# compiler code packed in the form of a typical
Visual Studio solution.

Share/Bookmark

This entry was posted in C#, C#5, Compiler Service, meta-programming. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://jorudolph.wordpress.com Johannes Rudolph

    (On a personal recurrent note, I still consider C# as being flawed since it doesn’t support non-nullable reference types.)

    The grass is greener in F# land (not only in this regard). It will strike back on you when interacting with other .NET code though. I’m currently learning F# (4 years of C# on my back now) and I’m really impressed by this language. Especially since it already has compiler as a service (F# interactive).

    When trying out a piece of code (e.g. checking behavior of a framework class) I find myself using f# interactive more and more often, even though my project is C#.

  • http://www.undisciplinedbytes.com Oliver Mezquita

    Pretty impressive evolution C# has had so far. I wonder what the future will bring …

  • Gert-Jan van der KAmp

    Clojure and Lisp are very clever, but until schools start teaching math in polish notation, I’m afraid they will never go mainstream.

    The first few minutes with a product determine if you will spend more with it, the polsih notation is just too akward for 99% of people.

    Having this metaprogramming in C# is excellent. I work on DSL all the time and that compiler as a service idea would be awesome. Does anyone have any detailed info on it yet or will it be the DLR integrated into C#?

    Also, for some reason people tend to forget dynamic sql as a form of metaprogramming. Personally i think that it’s by far the most used form of metaprogramming.

    Regards Gert-Jan

  • Gert-Jan van der KAmp

    Clojure and Lisp are very clever, but until schools start teaching math in polish notation, I’m afraid they will never go mainstream.

    The first few minutes with a product determine if you will spend more with it, the polsih notation is just too akward for 99% of people.

    Having this metaprogramming in C# is excellent. I work on DSL all the time and that compiler as a service idea would be awesome. Does anyone have any detailed info on it yet or will it be the DLR integrated into C#?

    Also, for some reason people tend to forget dynamic sql as a form of metaprogramming. Personally i think that it’s by far the most used form of metaprogramming.

    Regards Gert-Jan

  • http://codebetter.com/members/Patrick-Smacchia/default.aspx Patrick Smacchia

    Gautier, sure many languages support the non-nullable semantic. Personally I am mainly focused on C# at the time, mainly because I am responsible for hundreds of thousands of C# lines of code.

  • Gauthier Segay

    About nil checking, have you played with cobra language which support nil tracking semantic in it’s type system (String? <> String)?

  • Manoj Waikar

    I think DaRage is right, because that way C# creators can come up with versions 6, 7, 8…(and consequently VS 20XX…)

    If one is serious about meta-programming, LISP is THE language, and so is ClojureCLR (if one is tied to .Net). Clojure is already hot in the JVM world, but can’t say the same for ClojureCLR, but it is worth looking into.

  • DaRage

    Meta-programming will be a mojor paradigm shift if done. I feel that C# creators don’t won’t to give up so much control and keep feeding us features with a spoon.

    Automatic properties and code contracts are just but primitive implementation of meta programming. The real effect is being able to write full-fledged DSLs.

  • http://codebetter.com/members/Patrick-Smacchia/default.aspx Patrick Smacchia

    Peter, I am aware of CodeContracts capabilities.
    So far, because CodeContract didn’t exist, we used to use 10s of thousands
    Debug.Assert(param != null);
    which is indeed less efficient than CodeContract.

    Unfortunately, CodeContract solution is less elegant and more error prone that an extension of the type system that would be aware of non-nullable reference type, with the syntax TypeRef! to reference a non-nullable reference of TypeRef. The non-nullability would be here guaranteed at compile-time in any cases.

  • http://peterritchie.com/blog Peter Ritchie

    Technically, you can get pretty close non-nullable reference types with Code Contracts. For example:

    public static void Method(String text)
    {
    Contract.Requires(text != null);
    //…
    }

    Then, with the appropriate solution Code Contract settings, you’ll get a warning if null is ever passed to Method(String).

  • http://nesteruk.wordpress.com Dmitri

    Metaprogramming is really about getting code generated at a compilation stage. You can already get acquainted with metaprogramming by getting SharpDevelop and Boo, since this language supports it. Metaprogramming lays waste to any AOP framework you might think of because it let you inject code anytime, anywhere. Kind of like getting a CodeDom-like API for building code at compilation time.

    The real consequence of metaprogramming is that lots of people will create huge amounts of hackery (legitimate hackery, that is) around the idea of code-generation-within-code. Some tricky problems, like auto-implementation of INotifyPropertyChanged or turning every property into a dependency property or implementing multiple inheritance with a single attribute, are the kind of problems that will be solved with metaprogramming.