I took today a break from all the excitement around Windows Azure, clouds, and services, and I attended Anders Heilsberg's session about the future of C#. Oh boy, what a brilliant idea :) 70 minutes of pure joy ... ending up with what else than services?
Now, for those of you thinking C# reached its peak with version 3.0, you might think again.
Today's trends in programing languages can be expressed in three simple words: declarative, dynamic, concurrent. We're using more declarative stuff, we're interacting a lot with things that are not statically typed, and yes, we're running code on multicore machines. The most interesting of the three is clearly the dynamic aspect. Don't get me wrong, there's nothing wrong with the static approach. On the contrary. Static programming is a very good thing and it's here for the long shot. But things are changing. Take Silverlight for example and think about calling JavaScript. JavaScript is pretty cool, but static typing was never one of its strong points :)
As a matter of fact, the new paradigm in languages is ... well ... multi-paradigm. The truth is that we see languages borrowing from each other more and more. C# has now declarative aspects as well as functional ones. The purity of object orientation is a thing of the past. Of course, there's an endless debate whether this is a good thing or not, but the everyday facts are there and cannot be ignored.
C# 1.0 was about managed programming, C# 2.0 was about generics, and C# 3.0 was about language integrated queries. So what's in it for C# 4.0? Probably the most accurate word to describe it is DYNAMIC.
Before getting into the details, let's just think a moment about the .NET Dynamic Language Runtime. The DLR is based on a few key components like Expression Trees, Dynamic Dispatch, and Call Site Caching. The stuff that glues the DLR to the different environments is commonly referred as a binder (think about the object binder, JavaScript binder, Python binder, Ruby binder, or the COM binder). On top of the DLR, we already have stuff like IronPython and IronRuby. So what's next? ... You're right! C# and VB.NET :)
This leads to the first interesting thing in C# 4.0: dynamically typed objects. For those of you that have felt at least once in your life the pain of Type.InvokeMember or ScriptObject.Invoke, the new dynamic type introduced is going to be a bliss. All you need to do is declare a variable of type dynamic and you have dynamic method invocation. What does this actually mean? Well, to quote Anders, we now have variables that are statically typed to be dynamic :D. I suggest you read the previous sentence a few times before moving on :) :) I certainly did.
How does the code look like? Something like this:
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
Declaring calc as dynamic means that the invocation of the Add method is going to be performed dynamically, at runtime.
The second really cool thing is one that we've been crying for for a long time. You're right, I'm talking about optional and named parameters.
And of course, there are many more. To name just a few: the ability to derive from DynamicObject (becoming a player in the world of language dynamism), safe co-variance, safe contra-variance, greatly improved COM interoperability, and so on. I'll get back in the following weeks with more detailed posts about these features.
So, the story's complete: C# 1.0 was managed ;), 2.0 was generic, 3.0 was LINQ, and 4.0 is dynamic. Or... is it? Hell no! Anders and his team are already looking beyond 4.0 and what they see is nothing short of fantastic! In his talk today, he showed us just a small glimpse of things to come after 4.0. It was enough to get him a standing ovation at the end of the session!
What's the mystery feature? Common ... it's easy and obvious: it's about services. What kind of services? Compiler services, or, in other words the compiler as a service! The task at hand is to morph the black box that the compiler is today into a more transparent and accessible component. At the end of the day, this will enable you to compile pieces of source code on the fly and even use them in the context of your current app domain. In one word, amazing!
Oh yes, I almost forgot one key issue: it's the first time I heard a personality like Anders talking about language parity. It certainly looks like the C# vs. VB.NET competition's days are over.