Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!cs.utexas.edu!yale!husc6!spdcc!esegue!compilers-sender From: oliver@karakorum.berkeley.edu (Oliver Sharp) Newsgroups: comp.compilers Subject: Defining polymorphism vs. overloading Keywords: design Message-ID: <9008310419.AA06194@karakorum.berkeley.edu> Date: 31 Aug 90 04:19:43 GMT Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: oliver@karakorum.berkeley.edu (Oliver Sharp) Organization: Compilers Central Lines: 35 Approved: compilers@esegue.segue.boston.ma.us Once upon a time, shortly before an exam in a programming language course, I asked myself "exactly what is the difference between these two things?" The answer being "Ummm ... well ...", I decided to poke around. The result is that things seem a bit murky. It seems as though: o Overloading means using the same name (or symbol) to invoke different code depending on the types of the arguments (or operands). So, an example is +, which may do radically different things for integers, floats, and complex numbers. In other words, you have different chunks of code depending on the type. You may need to do some implicit casting, as in promoting an integer to a complex number, before you can actually perform the operation. o Polymorphism means using the same piece of code to operate on objects of different types. In LISP, cons is polymorphic because you can give it arguments of different types but it goes ahead and does its thing without worrying about it. Well, that seems pretty straight-forward so far, but in practice the distinction isn't so clean. For example, think about square braces in C. I've heard people say that they are overloaded, because they can apply to arrays of different types ... but, by our just agreed on definitions, they are really polymorphic. I'd look at someone funny if they said "braces are polymorphic in C", though, wouldn't you? I've also heard people describe things that are clearly overloaded as being polymorphic. Having just taken a Linguistics class from Lakoff, I could explain all this away with a lot of learned talk about radial categories, and prototypical cases, and so forth ... but I'm really curious. What do YOU think the distinction is? Or isn't there one? Do you use these terms consistently? - Oliver Sharp -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.