[Haskell-beginners] polymorphism vs. overloading

Christian Maeder Christian.Maeder at dfki.de
Fri Dec 17 11:26:31 CET 2010


Am 17.12.2010 04:34, schrieb Dennis Raddle:
> I looked up the difference between polymorphism and overloading
> (speaking of all languages in general), and apparently polymorphism is a
> general term for an operation that can accept arguments of different
> types. Overloading is a form of "ad-hoc polymorphism" in which the
> compiler chooses specific code to be executed by inspecting the
> arguments at compile time. Apparently "true polymorphism" means exactly
> the same code is executed no matter what the argument types, such as
> implemented by inheritance in C++ (*) Now my understanding of Haskell's
> classes is that the compiler chooses the right instance of the class at
> compile time. Does that mean that Haskell implements only ad-hoc
> polymorphism?

Usually three kinds of polymorphism can be distinguished:

- parametric p.
- inclusion p.
- ad-hoc p.

haskell supports parametric and a kind of "less ad-hoc" polymorphism
(via classes).

Plain object oriented languages support inclusion polymorphism.
Genericity or templates may introduce parametric polymorphism.
Typically, for numbers (integers vs. floats) ad-hoc polymorphism is
supported.

If the same or specific code is executed does not really matter,
parametric code can be specialized for some instances. The code for
different types can be the same, when the internal representation is the
same. And also the code for subtypes or subclasses can be inherited or
specialized.

HTH Christian

> (*) Another question: if someone says "What feature of C++ makes
> polymorphism possible?" What is the correct answer? Inheritance? Virtual
> methods? Some combination?

got it?

> 
> -D
> 



More information about the Beginners mailing list