Update: Exactly what I want, already implemented, complete with caching of the call site. Also, Microsoft’s Mads Torgersen responds to this suggestion here.
C# 4.0 proposes to add the dynamic keyword to the language. There are a few problems with it:
When the dynamic typing proposal for C# came out, my feedback was basically that you can already do it in the language today:
The justification for adding dynamic is that it gets rid of a lot of ugly reflection. But delegates and indexers mean that the language already has almost enough expressiveness to hide those details entirely.
Now the proposal is getting a lot firmer and there’s a CTP available, more people are voicing their concerns. I remain pretty unconvinced of the value of it. One good point made by several people now is that it ought to be possible to use an interface to describe the set of operations that an object must support. The object wouldn’t have to genuinely implement the interface – the IDE would just use the interface’s members to offer up auto-completion in the usual way. The compiler would then simply generate dynamic calls, so the interface would be irrelevant at runtime.
Charlie Calvert started a discussion way back in January about a proposal to add dynamic lookup support to C# – a convenient way to write code that makes calls into objects of a type not known at compile time.
A number of people have suggested that for the dynamic case, the member access syntax should be made deliberately different to the usual dot syntax. There is a lot to be said for this. The big danger of this whole idea is that a radically less type-safe way of working will start to creep into programs that used to be type-safe, thus undoing some of the good work done by adding generics. But as long as the syntax makes this clear, it’s better to have convenient support than nothing at all, or else people will invent their own approach and it will probably be horrible.
I was thinking that maybe to underline the fact that the method name is really no more solid than a string, you could have the syntax mimic accessing items in a dictionary.
So instead of: