C# 4 and the dynamic keyword - Whirlwind Tour around .NET 4 (and Visual Studio 2010) Beta 1
I've posted twice so far on .NET 4, first on ASP.NET 4, then on improvements in C# around dynamism and PIAs as well as the COM Binder. Now "dynamic."
So I asked this guy, what's up with the dynamic keyword, and what type was it exactly? I mean, C# isn't dynamic, right? He says:
"Oh, well it's statically-typed as a dynamic type."
Then my brain exploded and began to leak out my ears. Honestly, though, it took a second. Here's a good example from some of Ander's slides:
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);
That's the creation of an object, invokation of a method, and the collection of a return value. This is the exact same code, as the "var" type is figured out at compile time.
var calc = GetCalculator();
int sum = calc.Add(10, 20);
If you wanted to do the exact same thing, except with Reflection (like if it were some other class, maybe old-COM interop, or something where the compiler didn't know a priori that Add() was available, etc) you'd do this:
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
BindingFlags.InvokeMethod, null,
new object[] { 10, 20 });
int sum = Convert.ToInt32(res);
It's pretty horrible to look at, of course. If the object is some dynamic thing (from any number of sources), we can do this:
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
And get the dynamic method invocation and conversion of the return type. Basically it looks just like we're calling any other object.
Dynamism?
Here's the differences you see while coding. Hovering over the keyword gives me this nice tooltip.
When I hit the "." expecting intellisense to save me from my ignorance:
I'm told this is a dynamic expression that will be resolved at runtime.
Here's a C# program calling a method in a python (.py) file:
ScriptRuntime py = Python.CreateRuntime();
dynamic random = py.UseFile("random.py");
//Make an array of numbers
var items = Enumerable.Range(1, 7).ToArray();
random.shuffle(items);
Here we're passing in an array if ints (System.Int32[]) into the Python 'shuffle' method and it works just fine.
The DLR basically enables everyone to talk to everyone. That includes not just Python and Ruby, but Silverlight, Office/COM, and others.
What price REPL?
John Lam has a great post about his TechEd talk where he took a spin on a traditional REPL (READ-EVAL-PRINT-LOOP) using the DLR. He even allows switching back and forth between languages, which is odd/interesting.
John's even put the code for his REPL up on GitHub. Why is this interesting? Well...
He took his REPL and embedded it into an example Open Source app, specifically Witty, a WPF Twitter Client. Why he didn't use BabySmash is beyond me. ;) Check it out, as well as the source code diff for Witty on John's blog.
It'll be nice to have this kind of dynamic stuff just baked in and waiting for me to use it.
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Why he didn't use BabySmash is beyond me. ;)
Babies don't use Python or Ruby, they use LISP.
I have a persistent doubt, dynamic is just a "sintax sugar" for Reflection??
It's reflection plus a load of on-the-fly code generation and caching, so you don't pay the reflection costs repeatedly. So it's more than just syntactic sugar.
But please, please, please:
I CAN HAS method_missing. Please? KTHXBAI.
BTW, dynamic makes c# a hell of a lot closer to the message passing of Objective-C and Smalltalk, which is lovely. Methods are so last month.... all the cool kids are passing messages..... :)
It's reflection plus a load of on-the-fly code generation and caching, so you don't pay the reflection costs repeatedly. So it's more than just syntactic sugar."
Then a .Net 4 compiled program, can run in 3.5 runtime? Or have new MSIL codes?
I'm excited to try it. I just need a reason to use it.
Neither. A program compiled for .NET 4 contains dependencies on the .NET 4 assemblies, so it won't run with the 3.5 runtime
dynamic<ISomething> foo = CreateSomething();
...as a hint that, even though 'foo' is not statically guaranteed to implement the interface (or protocol, to be ObjC'y) ISomething, typeahead and compiler warnings could still help us out because we expect to use it in a certain context. Some compile-time checking is better than none...
The IDE performance isn't that good... but...
Comments are closed.
I had an old Chevy Chevette that used to vapor lock during the summer. That sentence did the same thing to my frontal cortex.