Re: Polymorphism vs. Overloading

nickb@harlequin.co.uk (Nick Barnes)
Wed, 9 Nov 1994 06:04:28 GMT

          From comp.compilers

Related articles
[29 earlier articles]
Re: Polymorphism vs. Overloading davidm@Rational.COM (1994-10-31)
Re: Polymorphism vs. Overloading bill@amber.ssd.csd.harris.com (1994-11-01)
Re: Polymorphism vs. Overloading drichter@pygmy.owlnet.rice.edu (1994-11-01)
Re: Polymorphism vs. Overloading bevan@cs.man.ac.uk (1994-11-02)
Re: Polymorphism vs. Overloading bimbart@CS.kuleuven.ac.be (Bart Demoen) (1994-11-02)
Re: Polymorphism vs. Overloading monnier@di.epfl.ch (1994-11-01)
Re: Polymorphism vs. Overloading nickb@harlequin.co.uk (1994-11-09)
Re: Polymorphism vs. Overloading franka@europa.com (1994-11-09)
| List of all articles for this month |

Newsgroups: comp.compilers
From: nickb@harlequin.co.uk (Nick Barnes)
Keywords: OOP, polymorphism
Organization: Harlequin Ltd, Barrington Hall, Cambridge UK
References: 94-10-144 94-11-007
Date: Wed, 9 Nov 1994 06:04:28 GMT

It seems that the object-oriented world and the functional world use
the word "polymorphism" in radically different ways. Please can we
stop making statements defining "polymorphism" and acknowledge this
fact? For instance this statement from Bill Leonard is just plain
false in the context of parametric polymorphism:


      94-11-007
      A polymorphic function uses the *dynamic* (read "runtime") type of
      an object, rather than its static type, to determine which function
      should be called.


There are three different sorts of functionality being discussed:


1. Overloading. We all seem to agree on what this means: the code
referred to by some identifier, such as "+", depends on the types of
the arguments, but the meaning can be discerned statically by the
compiler. This was once known (Strachey, 1967) as "ad-hoc
polymorphism".


2. Generic methods. This is widely used in the object-oriented world,
and in some OO languages this is referred to as "polymorphism". This
is like overloading, in that a single identifier can refer to several
different pieces of code depending on the type(s) of the
argument(s). However, the compiler cannot in general discern the
meaning at compile time and the selection between these different
pieces of code takes place when the function is called (at "method
despatch time") by examining the type of the argument. In some cases
the compiler may be able to optimise away this despatch overhead by
resolving the argument type at compile time (resolving trivia like "+"
in this way is important to performance in languages where numeric
values are full-blown objects).


3. Parametric polymorphism. This is widely used in the functional
world. The term dates back to Strachey again. Here the single
identifier refers to a _single_ piece of code, which can be applied to
many different types. The canonical example is the list length
function (in SML):


fun length [] = 0
    | length (x::xs) = 1 + (length xs)


This function has type "[forall 'a]('a list -> int)" (abbreviated to
"'a list -> int"), i.e. it can be applied to lists of any type, such
as [1,2,3] or ["a","b"].


Strachey, C. 1967, "Fundamental concepts in programming languages", in
      "Notes for the International Summer Schoold in Computer
      Programming, Copenhagen"


Nick Barnes, speaking for himself
--


Post a followup to this message

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