Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!rpi!bu.edu!husc6!spdcc!esegue!compilers-sender From: px@fctunl.rccn.pt (Joaquim Baptista [pxQuim]) Newsgroups: comp.compilers Subject: Re: Defining polymorphism vs. overloading Keywords: polymorphism Message-ID: Date: 3 Sep 90 22:09:26 GMT References: <9008310419.AA06194@karakorum.berkeley.edu> Sender: compilers-sender@esegue.segue.boston.ma.us Reply-To: px@fctunl.rccn.pt (Joaquim Baptista [pxQuim]) Organization: Universidade Nova de Lisboa -- Lisbon, Portugal Lines: 89 Approved: compilers@esegue.segue.boston.ma.us In-Reply-To: oliver@karakorum.berkeley.edu's message of 31 Aug 90 04:19:43 GMT I usually use the following definitions about "overloading", "polymorphism" and "generic code": - Overloading is the ability to have the same name (symbol) attached to different entities (functions, procedures, variables or whatsoever). - Polymorphism is the ability to have the compiler decide *at runtime* which entity to use depending on the runtime type (class or whatever) of the contents of variables. - Generic code is a chunk of code that has the ability of performing the same algorithm with more than one type. The actual types may be known at runtime (as in Smalltalk) or at compile time (as in Ada). Elaborating... The distinction between overloading and polymorphism is, for me, a matter of binding time. Overloading does not imply any runtime decision, it is just syntactic sugar for a family of different names that I, the programmer, usually collapse in one. No power is lost if individual names are used, ie, integer+ and float+. The same is not true with polymorphism. When I use (like in Smalltalk) "Graphic_object display", expecting an appropriate display procedure to be called for circles and rectangles, depending on what happens to be on the variable Graphic_object at runtime, I cannot use a circle_display and rectangle_display unless I use a case statement, as in (pascal-like): case Graphic_object is circle: display_circle(Graphic_object); rectangle: display_rectangle(Graphic_object); end; You may argue that it is still a matter of convenience and syntactic sugar, but I think that it is more than that. Some languages do not quite separate the concepts of overloading and polymorphism, with some confusing results (at least for me). I say that this is the case with c++. Examples of generic code are: - the same "Graphic_object display" (an uninteresting one, by the way); - a function to compute the lenght of a list (which does not depend on the type of its elements); - a function to sort some data structure (which depends on the existance of a <= relation between its elements). So, I say that the called functions are polymorphic functions, while the caller code is said generic. This genericity may involve dynamic binding (like in Smalltalk) or not (like the packages in Ada). In article <9008310419.AA06194@karakorum.berkeley.edu> oliver@karakorum.berkeley.edu (Oliver Sharp) writes: >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. Oliver Sharp seems to use polymorphic where I use generic, and uses overloading to mean my polimorphism with overloading. Or did I read it wrong? -- Joaquim Manuel Soares Baptista, aka px@fctunl.rccn.pt, px@unl.uucp Snail: CRIA, UNINOVA, FCT/UNL, 2825 Mt Caparica, Portugal -- Send compilers articles to compilers@esegue.segue.boston.ma.us {ima | spdcc | world}!esegue. Meta-mail to compilers-request@esegue.