Paulo Morgado

.NET Development & Architecture

Recent Articles

view all

Events

Projects

Recent Readers

Visitor Locations

Visitor Locations

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

C# Proposal: Compile Time Static Checking Of Dynamic Objects

C# 4.0 introduces a new type: dynamic. dynamic is a static type that bypasses static type checking.

This new type comes in very handy to work with:

Because static type checking is bypassed, this:

dynamic dynamicValue = GetValue();
dynamicValue.Method();

is equivalent to this:

object objectValue = GetValue();
objectValue
    .GetType()
        .InvokeMember(
            "Method",
            BindingFlags.InvokeMethod,
            null,
            objectValue,
            null);

Apart from caching the call site behind the scenes and some dynamic resolution, dynamic only looks better. Any typing error will only be caught at run time.

In fact, if I’m writing the code, I know the contract of what I’m calling. Wouldn’t it be nice to have the compiler do some static type checking on the interactions with these dynamic objects?

Imagine that the dynamic object that I’m retrieving from the GetValue method, besides the parameterless method Method also has a string read-only Property property. This means that, from the point of view of the code I’m writing, the contract that the dynamic object returned by GetValue implements is:

string Property { get; }
void Method();

Since it’s a well defined contract, I could write an interface to represent it:

interface IValue
{
    string Property { get; }
    void Method();
}

If dynamic allowed to specify the contract in the form of dynamic(contract), I could write this:

 

dynamic(IValue) dynamicValue = GetValue();
dynamicValue.Method();

This doesn’t mean that the value returned by GetValue has to implement the IValue interface. It just enables the compiler to verify that dynamicValue.Method() is a valid use of dynamicValue and dynamicValue.OtherMethod() isn’t.

If the IValue interface already existed for any other reason, this would be fine. But having a type added to an assembly just for compile time usage doesn’t seem right. So, dynamic could be another type construct. Something like this:

 

dynamic DValue
{
    string Property { get; }
    void Method();
}

The code could now be written like this;

 

DValue dynamicValue = GetValue();
dynamicValue.Method();

The compiler would never generate any IL or metadata for this new type construct. It would only be used, at compile time, for static checking of dynamic objects. As a consequence, it makes no sense to have public accessibility, so it would not be allowed.

Once again, if the IValue interface (or any other type definition) already exists, it can be used in the dynamic type definition:

 

dynamic DValue : IValue, IEnumerable, SomeClass
{
    string Property { get; }
    void Method();
}

Another added benefit would be IntelliSense.

I’ve been getting mixed reactions to this proposal. What do you think? Would this be useful?

Comments

Erik said:

"It would only thee used for compile type static checking of dynamic objects." - You use a speech to text engine to write your blog posts, don't you? =)

Otherwise - great ideas. It's unfortunate that the spec is pretty much fixed as it is. Perhaps we'll see some of this goodness in a hypothetical future version.

# March 17, 2010 8:57 AM

Paulo Morgado said:

Oh! You've heard me speak "English" before, haven't you?

In fact, I use a wireless Microsoft keyboard. It tends to impose its own filtering and sorting of keystrokes.

# March 17, 2010 10:06 AM

RichardD said:

Why not fake it by wrapping the dynamic object in a static class?

private sealed class DValue

{

   private readonly dynamic _value;

   public DValue(dynamic value) { _value = value; }

   pubilc string Property { get { return _value.Property; } }

   public void Method() { _value.Method(); }

}

DValue dynamicValue = new DValue(GetValue());

dynamicValue.Method();

# March 17, 2010 11:35 AM

Paulo Morgado said:

It's another idea.

# March 17, 2010 8:32 PM

Markus Tamm » Blog Archive » Links 18.03.2010 said:

Pingback from  Markus Tamm  » Blog Archive   » Links 18.03.2010

# March 18, 2010 4:40 AM

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - Mar 18-21, 2010 Web Development TDD kata for ASP

# March 18, 2010 7:29 AM

Anonymous said:

C proposal compile time static checking of dynamic objects.. Reposted it :)

# March 30, 2011 4:15 AM

weblogs.asp.net said:

C proposal compile time static checking of dynamic objects.. I like it :)

# April 22, 2011 7:33 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)