
On Wed, 2008-10-15 at 14:45 -0400, Stefan Monnier wrote:
The instance selection for an interface is done at run-time and this is inherently necessary. The instance (in a different sense) selection for type classes is almost always resolvable statically. In Haskell 98
In both cases, the dispatch is inherently dynamic, and in both cases, most dispatches can be resolved at compile-time with sufficient effort. The actual percentage may be quite different, tho. Implementation techniques are also fairly different, and the resulting coding style is also very different, but the two concepts are fundamentally very close to each other.
Rewrite this code so that there is no run-time remnants of dynamic dispatch, that is to say there is no run-time method look-up of any sort. You can assume that this plus an interface declaration for IDrawable and two classes Square and Circle is the whole program. int n = int.Parse(System.Console.ReadLine()); List<IDrawable> drawables = new List<IDrawable>(); for(int i = 0; i < n; ++i) drawables.Add(new Square()); drawables.Add(new Circle()); foreach(IDrawable drawable in drawables) drawable.Draw(); And exercise two: Write a Haskell example using type classes and not using existentials or polymorphic recursion where given the whole program dispatch is inherently dynamic.