(This response comes from the context of someone who like FP but has a day job writing in C++.) On Fri, 18 May 2001, Jerzy Karczmarczuk wrote:
We know that a good part of "top-down" polymorphism (don't ask me what do I mean by that...) in C++ is emulated using templates.
Umm... what do you mean by `top down'? The two senses in which I use polymorphism in C++ are: (i) using super classes (in the sence that A is a superclass of B is B inherits from A) to perform operations that only make sense for objects of the semantic type A. My canonical example is working with quadtree nodes, where a there are various types nodes which can hold different kinds of data but the base class qnode holds things like position, etc. (ii) using templates to write generic algorithms which either don't depend on the object type at all (e.g., vectors, lists, etc) or which depend on one or two conceptual operations which are very elementary (e.g., meaningful equality, addition or a `display yourself on stdout' function). In C++ these could _almost_ be done using superclasses but with problems because (A) the standard types (e.g., int) can't have their place in the class hierarchy redefined (e.g., int inherits from debug_show) (B) deep hierarchies are really not handled well by debuggers and it'd be a real pain trying to look at the C++ equivalent of the haskell `data A = ... deriving (Show,Ord,Monad,...)' The almost that I referred to above comes from the fact that (AFAIK) without using something nasty like RTTI you can't create new objects of the true type in a function to which you've passed a pointer to the base class. That's the conceptual overview. In terms of pragmatics __at least for my work in image processing__ I don't write classes that much, seldom use inheritance and I'm 99% sure I don't have any inheritance more than 1 level deep. On the other hand I write a lot of templated code which would be equivalent in haskell to either f :: a -> b -> .... -> a or f :: Eq a => a -> b -> ... -> a (i.e., algorithms that need just one or two low-level ideas such as equality). I much prefer the Haskell way of doing this with superclasses but I just don't feel brave enough to attempt the levels of class hierarchy that this implies in C++. (In some ways this is a shame since, because in template function names member functions used are only matched syntactically -- as there's no superclass to ensure semantic equivalence -- I've made one or two mistakes when I forgot a member function with the same name actually meant something different.)
Always when somebody mentions templates in presence of a True Functionalist Sectarian, the reaction is "What!? Abomination!!".
Now the question: WHY?
Why so many people say "the C++ templates are *wrong*" (and at the same time so many people use it every day...)
In my experience the C++ idiom `you only pay for what you use' (==> templates are essentially type-checked macros) and the fact most compilers are evolved from C compilers makes working with templates a real pain in practice. Additionally, I think a lot of people who dislike them are old-time C hackers who don't see why it isn't good enough to do polymorphism via void*'s and function pointers. As to why I at least use it, it lets me write polymorphic functions without needing deep class hierarchies (which as noted above aren't nice in C++) or going into the error prone mire of the void* approach.
Is it absolutely senseless to make a functional language with templates? Or it is just out of fashion, and difficult to implement?
I may be missing something obvious, but given Haskell's well thought out (IMO) prelude class structure and the fact that deep class hierarchies with `multiple inheritance' aren't a problem in Haskell, I don't see that it would buy you anything in Haskell. On the other hand, if the standard prelude didn't have the class hierarchy I think they would be much more useful. ___cheers,_dave________________________________________________________ www.cs.bris.ac.uk/~tweed/pi.htm|tweed's law: however many computers email: tweed@cs.bris.ac.uk | you have, half your time is spent work tel: (0117) 954-5250 | waiting for compilations to finish.