C# (4.0 ?) and dynamic lookups

Posted Sun, Mar 9 2008 12:01 by bill

Kudos to Charlie Calvert for opening up the discussion of C# future features to the public (sure would be nice if the VB team did likewise). In the first post on future features, Charlie talks about dynamic lookups in C# ....

Although the catchy term "Dynamic" is used here, there is in fact nothing dynamic about what is being proposed: you need to know the member name at compile time. The object that you might be calling it on can (has to ?) be late bound (as Object), but your code is still required to provide the exact name at compile time.  So it's not your code that is dynamic, it may be the code that creates the type you are working with is dynamic, but it doesn't have to be. An example given is Office or COM types where it's late bound. All that the C# team is really talking about is a subset of VB's Strict Off late bound capabilities. (more on that in a minute)

Dynamic for your code would be such as when you store the member name in a database or xml file etc, retrieve that then call that member using the string.  It's dynamic because it is created on the fly at runtime.  That's what dynamic means: objects that are in motion.  A typical example of this in VB today is using the CallByName function. Adding this capability to C# would be adding dynamic lookup; what Charlie proposes is late bound lookup ;)

Late bound lookup is useful when dealing with types that don't expose their member metadata at compile time (COM or DL types). There's actually a complex set of capabilities to make it "smooth" when working with such types apart from member calls, such as casting, assignment and operators. Member calls are just one issue. For C# the issue gets more complex as they have to decide if they are going to require case sensitivity on members, even though the object language or runtime (COM or .NET etc) might not be.

If C# is going to support the full gamut of features for working with dynamic types, then a block scope as proposed makes sense. Block scope is basically what VB has, but in VB the scope is the entire code file where Option Strict Off is specified.  That's a royal pain in the a***.  For VB however I'd much rather see fine granular control such as dynamic identifiers: foo.(bar) or similar kind of syntax to indicate a late bound call, and perhaps foo!("bar") for the dynamic call where the member name is supplied as a string.  And of course, the use of dynamic interfaces to allow the applying of contracts to your code would also be nice :)

What Charlie has shown for C# seems to me to be the worse of all worlds: it's not truly dynamic, and it's unclear/uncertain if C# will have any other support in those blocks for late bound operations other than member calls. That being the case I think C# would be better to have a modifier such as ! or .. instead of . to indicate which member calls are late bound. Block scope just allows lots of rooms for errors in code that can't be determined till runtime: that's the cost of late bound calls.  Limiting their use and scope in code should remain the common goal.

Filed under: , ,