Click here to Skip to main content
Licence CPOL
First Posted 13 Apr 2010
Views 8,210
Bookmarked 1 time

C# Proposal: Compile Time Static Checking Of Dynamic Objects

By | 15 Apr 2010 | Technical Blog
C# Proposal: Compile Time Static Checking Of Dynamic Objects
A Technical Blog article. View original blog here.[^]
free hit counters

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?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Paulo Morgado

Software Developer (Senior)
Paulo Morgado
Portugal Portugal

Member

About Paulo Morgado

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGo PinmemberQwertie4:59 21 Apr '10  
GeneralRe: Go PinmemberPaulo Morgado2:12 23 Apr '10  
GeneralRe: Go PinmemberQwertie4:24 23 Apr '10  
GeneralRe: Go PinmemberPaulo Morgado8:09 23 Apr '10  
GeneralRe: Go PinmemberQwertie16:07 23 Apr '10  
GeneralRe: Go PinmemberPaulo Morgado11:56 25 Apr '10  
GeneralRe: Go PinmemberQwertie18:10 28 Apr '10  
GeneralRe: Go PinmemberPaulo Morgado21:53 28 Apr '10  
GeneralWish-list item: narrowly-scoped extension types/functions Pinmembersupercat95:38 15 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions PinmemberPaulo Morgado12:47 15 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions Pinmembersupercat96:08 16 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions PinmemberPaulo Morgado14:40 16 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions Pinmembersupercat96:23 17 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions PinmemberPaulo Morgado10:45 17 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions Pinmembersupercat95:10 19 Apr '10  
GeneralRe: Wish-list item: narrowly-scoped extension types/functions PinmemberPaulo Morgado13:04 19 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 15 Apr 2010
Article Copyright 2010 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid