Encyclopedia > Type polymorphism

  Article Content

Type polymorphism

In computer programming, type polymorphism refers to various ways of allowing a function or procedure to operate on multiple types of objects.

There are several distinct concepts, all of which are referred to as polymorphism:

  • Parametric polymorphism - In this sort of polymorphism, a function is written generically so it can deal equally well with objects of various types. For example, an append function (one that takes two lists and appends them) can be written so it does not depend on the particular types of lists: it can append lists of integers, lists of real numbers, lists of strings, and so on. This was the first type of polymorphism developed, first identified by Christopher Strachey[?] in 1967. It was also the first type of polymorphism to appear in an actual programming language: ML in 1976. It exists today in Standard ML[?], O'Caml[?], Haskell, and others. Some also argue that templates should be considered an example of parametric polymorphism, though they rely on macros to generate specific code rather than actually reusing generic code.
  • Ad-hoc polymorphism is better-known as operator overloading. This form of polymorphism allows multiple functions with the same name, but taking different types, to be defined; the compiler automatically calls the right one. With operator overloading, functions appending lists of integers, lists of strings, lists of real numbers, and so on could be written, and all be called append, and the right append function would be called based on the type of lists being appended. This differs from parametric polymorphism, in which only one generic function needs to be written. Some argue that ad-hoc polymorphism is not polymorphism in a meaningful computer science sense at all, and is just a shorthand for the programmer calling append_integer and so on manually. This sort of polymorphism is most common in object-oriented programming languages.
  • Subtyping polymorphism allows a function to be written so that it takes a certain type of object, but will work correctly if passed an object that is a subtype of the object it expects. For example, if in a certain programming language with subtyping polymorphism there were a type "Number", of which "Integer" and "Real Number" were subtypes, a function could be written so that it takes a Number, and works equally well whether an Integer or Real Number is passed. In object-oriented programming this is implemented with subclassing (inheritance), and is also known as late binding. See also Polymorphism in object-oriented programming.



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
North Haven, New York

... average family size is 2.77. In the village the population is spread out with 17.4% under the age of 18, 3.5% from 18 to 24, 22.3% from 25 to 44, 28.7% from 45 to 64, and ...

 
 
 
This page was created in 34.3 ms