Mike Chapman <Mike.Chapman@muc.de>Mon, 31 Oct 1994 22:23:00 GMT

From comp.compilers

Related articles
[9 earlier articles]
[15 later articles]
| List of all articles for this month |

 Newsgroups: comp.compilers From: Mike Chapman Keywords: polymorphism Organization: Compilers Central References: 94-10-144 94-10-154 Date: Mon, 31 Oct 1994 22:23:00 GMT

Gabriela O. de Vivo <gdevivo@conicit.ve> wrote:

>Last week I was invited to join a Thesis (MsC) presentation.
>At some point a question raised about the exact difference between

Joseph H Allen (jhallen@world.std.com) wrote:
: The difference is purely syntactical. Calls to overloaded functions look,
: well, like function calls. Calls to polymorphic functions require a dot or
: '->' somewhere. Really, that's the only difference. Artificial semantic
: restrictions placed by certain languages aside, you can always move the
: identifier or address-expression from the left of the dot into the
: parenthasis as the first argument to generate an equivelent overloaded
: function call.

No, I think this is wrong.

The big difference is that how the selection of the function is done.

The selection of an overloaded function occurs statically based upon
the declared types of the parameters.

The selection of a polymorphic function depends on the types of the
object (the parameter before the '.' or '->' in most languages)
at the time of invocation.

So if you have Shape objects of which Rectangles and Circles
are specific kinds of shapes and have say an overloaded
function display and a polymorphic function draw then

after

Shape x,y;
Rectangle r;
Circle c;

x := r; -- Rectangles and circles are shapes
y := c; -- so these assignments are type safe.

display(x) means Shape's display
display(y) means Shape's display
display(c) means Circles display
display(r) means Rectangles display

Then for the polymorphic function draw

x.draw means Rectangles draw
y.draw means Circules draw
r.draw means Rectangles draw
c.draw means circles draw

All (?) OO languages support the polymorphic version.
Almost all OO languages support only one special argument
which controls the dispatching (the one before the '.' or
'->'). As far as I am aware Ada 9x allows dynamic dispatching
on more than one argument and does not use the '.' notation.

names for some of your functions and has no real conceptual
significance. You could just have well have written
display_circle(c)
display_rectangle(r)
etc.

Smalltalk supports only the draw kind of call,
Ada 83, Algol 68 support only the display kind of call.
'C' has overloaded standard operators (eg '+' on int's
unsigned ints, double's etc).