Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!snorkelwacker!mintaka!spdcc!esegue!compilers-sender From: pcg@cs.aber.ac.uk (Piercarlo Grandi) Newsgroups: comp.compilers Subject: Re: Defining polymorphism vs. overloading Keywords: polymorphism Message-ID: Date: 13 Sep 90 12:16:23 GMT References: <8133@amelia.nas.nasa.gov> <12977@june.cs.washington.edu> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: Piercarlo Grandi Organization: Coleg Prifysgol Cymru Lines: 70 Approved: compilers@esegue.segue.boston.ma.us In-Reply-To: pardo@cs.washington.edu's message of 7 Sep 90 16:52:16 GMT On 7 Sep 90 16:52:16 GMT, pardo@cs.washington.edu (David Keppel) said: >[Ongoing discussion of overloading and polymorphism] > To summarize: > Given the name of of an operator where that operator applies to > several types, the operator is > * Polymorphic if all implementations are derived from a single > specification. > * Ad-hoc polymorphic or overloaded if the implementations are derived > from several specifications. > Oversimplified, but close? Agreeably so, under current definitions. However the entire discussion about *understanding* polymorphism and overloading (as well as defining them), is seriously vitiated by one problem, that is always present: why would we want to have overloading or polymorphism? This is a less stupid question than it looks. The answer is *reuse*. My usual mantra is worth repeating here: What we really want is to be able to express notationally: * reuse of interface * reuse of semantics * reuse of implementation These are very different concepts. The various *mechanisms* that are related (overloading, polymorphism, generic packages, private scopes, inheritance, delegation, ...) do not directly address the support of these concepts. Just to go back to the mechanisms at hand, overloading is reuse of part of an interface, and to a certain extent overloading of part of semantics; when we have + overloaded for ints and floats the name is reused, and so is part of the semantics, in that the mathematical properties are assumed to be similar (hintsd of category theory). Things start to look fishy when we overload + for string concatenation, as the reuse of semantics thing starts to look very flimsy. Note that reuse of interface is also partial here, because all these overloadings are actually defined over two operands of the same type, even if the type is different each time. Polymorphism is reuse of implementation, with some hints of reuse of semantics; for example you can use much the same logic to calculate the length of a string and that of a file. Of course this relies on reuse of semantics, because it will usually happen that the semantics of the types over which we are polymorphic are somewhat similar. But, and this rarely considered, it may happen that the same implementation (shape of code) can be applied with entirely different results to different types; this is especially obvious with functionals. Polymorphism also implies some partial reuse of interface, as using the same implementation more or less implies using the same interface. What I would really like is for language designers to provide notation to directly address the three distinct aspects of reuse, not with non orthogonal and clumsy solutions. -- Piercarlo "Peter" Grandi | ARPA: pcg%uk.ac.aber.cs@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcsun!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.