All Your Base Are Belong To Us

Mostly .NET internals and other kinds of gory details

PDC Day 1: The Future of C#

Even though there are 7 people from Sela at the PDC (impressive considering the Israeli delegation size of around 20 people), we all have very different interests when it comes to choosing a session to attend.  However, we were decisively unanimous when it came to Anders Hejlsberg's session titled The Future of C#.

Anders Hejlsberg is just that - Anders Hejlsberg.  In an amazing presentation he covered the future of C# - what's going to be in the upcoming C# 4.0 release and what the future blueprints for C# vNextNext are going to be.

I'm sure this is going to be covered in the blogosphere, and covered again in my own posts - but here's the summary of features in the next release of C# - with the general saying that C# 4.0 is an enabler of dynamic programming paradigms.

Dynamic programming is enabled first and foremost through a new language keyword - dynamic.  A variable that is of type dynamic exhibits runtime binding of method calls and cast operations.  The binding itself is performed using pre-built or custom binders that invoke the operation.  For example, binders are available for scripting languages such as IronPython, IronRuby and JavaScript, for COM interoperability, for XML access and various other purposes.

The following piece of code demonstrates what is now possible:

dynamic d = GetSomethingFromSomewhereAtRuntime();
int i = d.Foo(15);    //Implicit return value cast from type 'dynamic'

This code will work for a COM object that has a method called Foo which takes an integer and returns an integer, it will work for a simple type that has this method, it will work for an object obtained from a scripting language such as JavaScript, and anything else that can be bound to at runtime.

A dynamic invocation is not necessarily a performance hit - call site caching and JIT tricks can result in a slow first call, but once the binding has been made, it can be invoked repeatedly without additional binding hoops.

This has lots of exciting applications, including the ability to provide generic operations without constraints - if the resolution is bound to runtime, the compiler won't nag you about missing constraints (and remember that some things such as operators are not available without constraints).

Other features Anders talked about are slightly less exciting, but still interesting on their own.  The first one was optional and named parameters finally being available in C# 4.0.  It's amusing that it took the C# language architects several years to realize what Bjarne Stroustrup engineered into the very first versions of C++, but optional parameters will significantly simplify VB and COM interop and end the annoyance of having a primary method with a gazillion parameters and dozens of other methods that accept a subset of parameters and call the primary method.  Named parameters will make method calls more readable (even without the Intellisense tooltip), which is always a welcome addition.

public static void LogOn(
    string username = "",
    string password = "",
    string domain = "");

LogOn("Sasha");
LogOn(domain: "Sela");
LogOn(domain: "Sela", username: "Sasha");
LogOn("Sasha", "Password", "Sela");

The next thing Anders discussed was covariance and contravariance with generic types - which is something that Eric Lippert has mentioned in multiple posts in the past while architecting this language feature.  Essentially, it means that generic interfaces can be decorated in such a way that will enable output parameters to be covariant and input parameters to be contravariant, so that it's OK to pass an IEnumerable<Zebra> to a method that accepts an IEnumerable<Animal> if the IEnumerable<T> interface was declared as covariant.

public class Zoo {
    IEnumerable<Animal> GetAnimals() {
        return new List<Zebra>(...);
    }
}

Finally, Anders briefly mentioned NoPIA - type equivalence and type embedding - which are features I will discuss in a later post.

At the very end of the talk, Anders showed off a couple of features that are likely to make into the next version of C# (after C# 4.0).  He mainly demonstrated the concept of "Compiler as a Service" - the ability to use compiler services in an intuitive manner and to gain access to the compiler's AST and its object model.  He showed a simple read-eval-print loop that provides semantics similar to interactive shells like Python and PowerShell - but all in C#.

To summarize, there are lots of exciting changes to the language which will primarily make it multi-paradigm, enabling dynamic and static typing patterns to further evolve in C#.

Oh, and I forgot to mention - I just got my hands on "The Goods" - a WD 160GB Passport drive with over 50GB of VPC images, walkthroughs, SDKs and other goodies.  I'll keep you posted as I explore the vast amount of material we are all going to deal with in the next few months.

Comments

ShaharY said:

What happens if the GetSomethingFromSomewhereAtRuntime method will return an int? Will the application crash?

If it will, I really don't like this dynamic keyword, it leaves a huge space for hiden mistakes.

"This code will work for a COM object that has a method called Foo which takes an integer and returns an integer, it will work for a simple type that has this method" - implementing an interface will do the same job...

# October 29, 2008 3:41 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: