« The Weekly Source Code 42 - Tree Trim, P... | Main | Back to Basics: Using Fusion Log Viewer ... »

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.



Wednesday, May 20, 2009 2:20:24 PM (Pacific Standard Time, UTC-08:00)

"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 2:57:29 PM (Pacific Standard Time, UTC-08:00)

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


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

I have a persistent doubt, dynamic is just a "sintax sugar" for Reflection??
Wednesday, May 20, 2009 6:06:54 PM (Pacific Standard Time, UTC-08:00)
can you return dynamic?
anonymouse
Thursday, May 21, 2009 12:37:14 AM (Pacific Standard Time, UTC-08:00)
"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 1:26:43 AM (Pacific Standard Time, UTC-08:00)
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 2:36:23 AM (Pacific Standard Time, UTC-08:00)
""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 12:03:36 PM (Pacific Standard Time, UTC-08:00)
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 2:13:19 PM (Pacific Standard Time, UTC-08:00)
"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
Friday, May 22, 2009 10:27:38 PM (Pacific Standard Time, UTC-08:00)
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 6:37:24 AM (Pacific Standard Time, UTC-08:00)
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
Comments are closed.

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (5)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads