Friday, December 26, 2008

C# Becomes a Dynamic Language...

This post is a bit late - in fact this news was announced several months ago at this year's Microsoft Professional Developer's Conference.

At the PDC the Dynamic Language Runtime was demonstrated - along with the news that it will be part of .NET 4.0. Both Visual Basic 10 and C# 4.0 will have dynamic elements that make use of the Dynamic Language Runtime.

In a blog entry after the conference Jim Hugunin links to Channel 9 videos of his talk on the DLR and Anders Hejlsberg's talk on the way C# is changing.
C# 4 will allow you to use objects dynamically through a new static declaration. A new keyword that allows you to statically declare dynamic objects!

Objects that are declared as dynamic will have operations on them (attribute lookups, method calling, indexing, numerical operations and so on) done through the DLR.

The intarwebz were awash with the news. Here is a selection of the most interesting and informative responses that I found:
You can try this out with CTP (Community Technology Preview) release of Visual Studio 2010 and a version of IronPython 2.0 (pre-IP 2 final) that targets it:
What are the benefits of having dynamic objects (or even static objects that implement the new IDynamic interface)?

Well, you could declare everything as being dynamic, and then C# effectively would be a dynamically typed language with duck typing - except unlike most dynamic languages you still have a lot of type declarations and you have a compile phase with the DLR used to perform operations through reflection.

Substantially more useful is that it makes integrating code in DLR based languages into a .NET application substantially less painful. Although the DLR hosting API is generally very straightforward to work with, passing objects from IronPython or IronRuby back to C# can be a problem. You have to declare ahead of time what type it will be - and if it is a Python or Ruby object that class won't even exist until runtime. You can now declare them as dynamic objects, and the DLR will use the semantics of the language they are defined in when it uses them. Your dynamic objects will still behave like Python or Ruby objects when used from C#.

The DLR can also be used for late-bound COM, something that hasn't really been possible from C# before. It can also be used to create really fluent APIs when working with things like XML, JSON or an HTML object DOM. Dynamic languages have been able to do object traversal with element names as attribute names for many years - and the equivalent in statically typed languages is more verbose and ungainly.

The C# futures documentation gives this example of using the new dynamic keyword:

dynamic d = GetDynamicObject(…);
d.M(7); // calling methods
d.f = d.P; // getting and settings fields and properties
d[“one”] = d[“two”]; // getting and setting through indexers
int i = d + 3; // calling operators
string s = d(5,7); // invoking as a delegate

It also has this to say:

"The type dynamic can be thought of as a special version of the type object, which signals that the object can be used dynamically. It is easy to opt in or out of dynamic behavior: any object can be implicitly converted to dynamic, “suspending belief” until runtime. Conversely, there is an “assignment conversion” from dynamic to any other type, which allows implicit conversion in assignment-like constructs:"

dynamic d = 7; // implicit conversion
int i = d; // assignment conversion

There's lots more in C# 4 - including named and optional parameters - it's shaping up to be a nice language, for a statically typed one...

1 comment:

  1. Wow, that is some pretty cool stuff dude!

    Jess
    www.privacy-tools.at.tc

    ReplyDelete

Note: only a member of this blog may post a comment.