Scott Hanselman

C# 4 and the dynamic keyword - Whirlwind Tour around .NET 4 (and Visual Studio 2010) Beta 1

May 21, 2009 Comment on this post [15] Posted in DLR | Learning .NET | TechEd
Sponsored By

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.

image

When I hit the "." expecting intellisense to save me from my ignorance:

image 

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.

image

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...

Screenshot of John Lam's REPL/editor

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.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
May 21, 2009 2:20

"Oh, well it's statically-typed as a dynamic type."


I had an old Chevy Chevette that used to vapor lock during the summer. That sentence did the same thing to my frontal cortex.
May 21, 2009 2:57

Why he didn't use BabySmash is beyond me. ;)


Babies don't use Python or Ruby, they use LISP.
May 21, 2009 3:24
Only the innocence and untainted brain of a child could grok all of those parentheses.
May 21, 2009 4:21
Is JavaScript supported by the DLR as well?
May 21, 2009 5:21
"Reflectively Extensible Programming Languages and System"? Or "read-eval-print loop"?
May 21, 2009 5:41
First sorry for my bad english.

I have a persistent doubt, dynamic is just a "sintax sugar" for Reflection??
May 21, 2009 6:06
can you return dynamic?
May 21, 2009 12:37
"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.
May 21, 2009 13:26
dynamic look good. Well, it looks like a variant and late binding from the COM days, but hey - better than nothing.

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..... :)
May 21, 2009 14:36
""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."

Then a .Net 4 compiled program, can run in 3.5 runtime? Or have new MSIL codes?
May 22, 2009 0:03
I used to think the var keyword was the end of c#, or my life. Now I cant live without it.

I'm excited to try it. I just need a reason to use it.
May 22, 2009 2:13
"Then a .Net 4 compiled program, can run in 3.5 runtime? Or have new MSIL codes?"

Neither. A program compiled for .NET 4 contains dependencies on the .NET 4 assemblies, so it won't run with the 3.5 runtime
May 23, 2009 10:27
Hmm... I'd be a lot more interested in 'dynamic' if we had
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...
May 26, 2009 18:37
So...basically what you are saying is the C# is getting Variants. I think we got those in 1993 with VB3. All of you C-type language people used to call VB a toy language until you realize that it actually has some useful features... :)
February 07, 2010 2:08
.NET 4 brings over great stuff... I've been working with b2 and it looks quite good.
The IDE performance isn't that good... but...

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.