Scott Hanselman

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

May 20, '09 Comments [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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Wednesday, May 20, 2009 10:20:24 PM UTC

"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.
KevDog
Wednesday, May 20, 2009 10:57:29 PM UTC

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


Babies don't use Python or Ruby, they use LISP.
Wednesday, May 20, 2009 11:24:32 PM UTC
Only the innocence and untainted brain of a child could grok all of those parentheses.
KevDog
Thursday, May 21, 2009 12:21:11 AM UTC
Is JavaScript supported by the DLR as well?
Thursday, May 21, 2009 1:21:14 AM UTC
"Reflectively Extensible Programming Languages and System"? Or "read-eval-print loop"?
Stuart
Thursday, May 21, 2009 1:41:22 AM UTC
First sorry for my bad english.

I have a persistent doubt, dynamic is just a "sintax sugar" for Reflection??
Thursday, May 21, 2009 2:06:54 AM UTC
can you return dynamic?
anonymouse
Thursday, May 21, 2009 8:37:14 AM UTC
"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.
Will
Thursday, May 21, 2009 9:26:43 AM UTC
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..... :)
Thursday, May 21, 2009 10:36:23 AM UTC
""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?
Thursday, May 21, 2009 8:03:36 PM UTC
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.
Thursday, May 21, 2009 10:13:19 PM UTC
"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
Saturday, May 23, 2009 6:27:38 AM UTC
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...
Steve Kradel
Tuesday, May 26, 2009 2:37:24 PM UTC
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... :)
Jeremy
Saturday, February 06, 2010 10:08:14 PM UTC
.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.