Re: Defining polymorphism vs. overloading

px@fctunl.rccn.pt (Joaquim Baptista [pxQuim])
3 Sep 90 18:09:26

          From comp.compilers

Related articles
Defining polymorphism vs. overloading oliver@karakorum.berkeley.edu (1990-08-30)
Re: Defining polymorphism vs. overloading burley@world.std.com (1990-09-01)
Re: Defining polymorphism vs. overloading wright@gefion.rice.edu (1990-09-02)
Re: Defining polymorphism vs. overloading compilers-sender@esegue.segue.boston.ma.us (1990-09-03)
Re: Defining polymorphism vs. overloading hrubin@l.cc.purdue.edu (1990-09-03)
Re: Defining polymorphism vs. overloading px@fctunl.rccn.pt (1990-09-03)
Re: Defining polymorphism vs. overloading plph@caen.engin.umich.edu (1990-09-03)
Re: Defining polymorphism vs. overloading daveg@near.cs.caltech.edu (1990-09-03)
Re: Defining polymorphism vs. overloading dmw9q@uvacs.cs.Virginia.EDU (1990-09-05)
Re: Defining polymorphism vs. overloading pase@orville.nas.nasa.gov (1990-09-06)
Re: Defining polymorphism vs. overloading tub!wg@relay.EU.net (1990-09-06)
Re: Defining polymorphism vs. overloading pase@orville.nas.nasa.gov (Douglas M. Pase) (1990-09-06)
[11 later articles]
| List of all articles for this month |
Newsgroups: comp.compilers
From: px@fctunl.rccn.pt (Joaquim Baptista [pxQuim])
In-Reply-To: oliver@karakorum.berkeley.edu's message of 31 Aug 90 04:19:43 GMT
Keywords: polymorphism
Organization: Universidade Nova de Lisboa -- Lisbon, Portugal
References: <9008310419.AA06194@karakorum.berkeley.edu>
Date: 3 Sep 90 18:09:26

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
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.