Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!snorkelwacker.mit.edu!ai-lab!life!tmb From: tmb@ai.mit.edu (Thomas M. Breuel) Newsgroups: comp.lang.c++ Subject: Re: Common data structures Message-ID: Date: 19 Jun 91 03:50:04 GMT Sender: news@ai.mit.edu Organization: MIT Artificial Intelligence Lab Lines: 71 In-reply-to: pcg@aber.ac.uk's message of 14 Jun 91 17:39:52 GMT In article pcg@aber.ac.uk (Piercarlo Grandi) writes: bill> I would agree with Dag Bruck that any real list package must bill> let objects be members of multiple lists I believe that this particular example is the core of the difficulties of making a good reuse oriented language. The problem is that C++'s version of polymorphism is rather inconvenient to use. In C++, you must introduce type names in templates and specify them at the point of use even if the compiler has enough information to infer this information (I don't have a C++ compiler with templates, so the syntax that your C++ implementation uses for templates may differ slightly from what I am using in this example): template list map(base (*f)(base),list l) ... ; ... map(f,l); By contrast, in ML, you can simply write: fun map(f,l) = ... ... map(f,l) or fun map(f:'base -> 'base,l:'base list): 'base list = ... ... map(f,l) Note that when using the function "map" in ML, you never have to specify what the value of the type variable "'base" actually is; the type checker figures it out by itself. This means that in ML you only need to specify types if you want a function to be monomorphic rather than polymorphic, or to resolve ambiguities in operator overloading. In fact, Scheme and Lisp go one step further: "generic" arithmetic and other "generic" operations are provided via dynamic typing rather than via overloading, so you don't ever need to declare types to resolve overloaded functions. The issues are very subtle, and only using pointers solves them, if by gross oversimplification, in current languages. It is true but not sufficient to say that: ML certainly does not force the programmer to use pointers in order to achieve polymorphism (in fact, ML does not have "pointers" in the usual sense). However, many implementations of ML use pointers internally to implement polymorphism. But, implementations that are concerned with optimizing data space and execution time at the cost of program size and compilation time do not need to be based on pointers; my understanding is that SISAL 2.0 is an example of a polymorphic language that does not use pointers for implementing polymorphic functions (it probably generates different versions of functions for different argument type patterns). In summary, statically typed languages with efficient implementations of polymorphism are possible. The C++ programming language, however, was not designed with this in mind, and I doubt if this is easily fixable. In particular, polymorphism and overloading interact in unpleasant ways. If you want the convenience of a fully polymorphic type system, you are probably better off using a language that was designed with such a type system in mind from the ground up. Thomas.