Its an interesting idea, and one that has been explored before. I'll link to some research and existing work, after I give you my take. I'll talk about scripting languages and static imperative languages, since I think this is what you're talking about.
The fundamental problem is that types don't mean the same thing in static and dynamic type systems. In a dynamic system, a type is a property of a value, and as the value is passed around, so is the type. In a static system, you restrict the types of values which may be held by variables (etc). So far so good.
Static types in dynamic languages
The problem occurs when you look at how objects are used in scripting languages - they are all duck-typed. This means the name of the type (nominal-typing) isn't useful. All modern imperative languages (Java, C#, C++, C) use nominal type systems. So in C++ you might say you expect a Duck
, but in Python you really want something that goes quack()
.
So consider adding static typing to a duck-typed language. Since a function will expact a parameter to quack()
, but it can't say that it expects a Duck
, combining the two is difficult. You might define an interface called quacks
which can quack()
, and use that as the type. But this is a tad verbose really, which kills the benefit of the dynamic typing. Perhaps though, there might be something along these lines (some kind of structural type system) which can do it.
An alternative would be to just require the programmer to specify Duck
, and damn this duck-typing business anyway - no one really uses it do they? But then you're just writing Java in Python, and as someone who tried that once, let me tell you its very counter-productive.
Dynamic types in static languages
So lets look at it the other way. How will C# benefit from the dynamic
keyword? The simple answer is that it won't. I honestly don't see any of the beauty and freedom that you get from Python in C#'s dynamic
. Now, the only thing I know about it comes from a talk by Jon Skeet, but the overwhelming impression I got is that it's verbose and inelegant. And I think that's not an implementation error from the C# folk. I think its because the problems which dynamic typing solves in Python are already solved in C# (albeit verbosely), and dynamic
just brings nothing to the party.
Research on static/dynamic stuff
Look up Jeremy Siek's gradual typing stuff, its about the most advanced static/dynamic research out there. Its a bit hard reading though, and I've only given it a cursory look myself, so I can't summarize it. However, its interesting to flick through his related work alone, and the STOP conference will probably have good stuff in it.