Jeffrey Palermo, CTO of Headspring Systems, proposes a compromise in using dynamic for C#: the ability to make an entire method dynamic while keeping assemblies static.
Commenting on a recent debate on Static vs. Dynamic Typing, Palermo noted that dynamic programming is hard when it comes to use libraries because:
you really don’t know what it supports except if the documentation is liberal and flawless. There are no interface types to describe what properties and methods will be called once you pass the object in. If the documentation doesn’t cover every API, you are forced to write characterization tests.
Another problem is, in his opinion:
When working with a dynamic language, you have to keep more of the system in your head in order to program. You have to remember which object is overriding which method, and what objects have new functionality attached to them.
Both of these problems are real issues when having to deal with external libraries, but they are not such a problem for a developer working in his own code because he owns the code.
C# 4.0 introduces dynamic lookup allowing developers to declare objects of dynamic type. This feature was intended to let programmers more easily deal with objects from dynamic languages such as Python or Ruby, COM objects accessed through IDispatch, objects with a changing structure like HTML DOM or .NET types obtained via reflection. Objects of dynamic type can be used with some limitations as they usually are in dynamic languages.
Palermo proposes the introduction of a dynamic keyword in C# that would allow dynamic typing for all the objects in the method without having to explicitly declaring each as dynamic. This would allow developers who prefer dynamic typing to use it inside C# and in the meantime would leave the language static at the assembly boundary level for safety.
Is this a desirable compromise?