waffle

dynamic

It’s been kicking around for a while, but the first C# 4.0 keyword has been confirmed: dynamic. (In the slides to Anders Hejlsberg’s “Where Are Programming Languages Going?” keynote at JAOO.)

The code sample presented is this:

dynamic calc = GetCalculator(); 
int sum = calc.Add(10, 20); 

The slide presents calc as being “statically typed to be dynamic”, the second row’s assignment as being a “dynamic conversion [to int]” and involving a “dynamic method invocation”. I can step to most of that. The parts that worry me are:

  • The var keyword and anonymous types worked perfectly with the existing type systems — the types exist and they are reproducibly the same at compile-time unless you change the initial assignment, you just can’t name them. Dynamism, on the other hand… What does dynamic calc; var type = typeof(calc); return?

  • Can you make methods that pass dynamic parameters? Can you make a generic class with dynamic parameters? (If not, are you just supposed to stuff these into ArrayLists or List<object>s.)

  • Consider a dynamic method that returns an array of two objects, both of distinct types. Chain the method call to an extension method on IEnumerable<T> that returns either the first or last object randomly — what type is a variable that calls this method? If you were to call this again, would the variable type change?

  • Can you write code that calls plain old CLR objects using dynamic? — crippling the code of everything that’s useful and efficient, sure, but avoiding a rift in the type system.

The big question is eventually “is this a compile-time alias (like var) or something that delivers a deeper change (invokedynamic-like) to the CLR?” My guess is that the usage suggests a generated interface. For the code sample above, there might be something like — and at any rate this might be what the IntelliSense store needs to remember:

interface <>_GeneratedDynamicInterface { int Add(int a, int b); }

(The int parameter types are simply because integer literals are 32-bit integers if they don’t exceed the range or carry suffixes like ‘u’ or ‘L’.)

Meanwhile, other parts of the slides suggests that it’s just going to wind up as the same thing rewritten in reflection calls (to things like InvokeMethod, only resolved a lot more resolutely thanks to the information the compiler can deduce in the same way that it finds the correct overloads for, uh, non-dynamic method invocation).

I have absolutely no idea how it’s going to work, though. It’s going to be very interesting to find out.

Moreover, I advise that the iPhone software platform must be opened.

Comments [+]

  1. Ceterum censeo Carthaginem esse delendam, eh?

    By rsfinn · 2008.10.07 04:01

  2. Sic.

    By Jesper · 2008.10.07 07:07

  3. There has been a discussion going on about ‘Dynamic Lookup’ some time ago over at Charlie Calverts Future Focus http://blogs.msdn.com/charlie/archive/2008/01/25/future-focus.aspx

    Dynamic keyword was mentioned a lot

    By Anastasiosyal · 2008.10.07 12:49

  4. That’s interesting; I read that and I’ve always been under the impression that it’d be a block just like (un)safe or (un)checked which changes the mechanics of that code. But now it’s also a qualifier, which asks the question: is it just an implicit dynamic block around every line where that object is used (for convenience), or does it actually carry entirely new semantics beyond what the block would do?

    By Jesper · 2008.10.07 18:38

Leave a comment

Your e-mail address is never shown. If you type a line break in the comment, it will show up as a line break (naturally). The following HTML is allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(required)

(required)


Please note: Your comment will not show up at once. Unless you're spamming or being abusive, you have nothing to worry about. (Read the full policy.)