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.
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:
- Mike Hadlow: Dynamic Dispatch in C# 4.0
- Dare Obasanjo: C# is the Next Python: Duck Typing and C# 4.o
- Keith Hill: C# 4 - it's a Dynamic World after all
- VB Team: Walkthrough: Dynamic Programming in Visual Basic 10.0 and C# 4.0
- C#, F# and Other Programming Language Fun at PDC 2008
- A First Look at Duck Typing in C# 4.0
- Visual Studio 2010 and .NET Framework 4.0 CTP
- IronPython 2 for the VS 2010 CTP
- C# Future - October CTP Release
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...