A powerful expressive form that allows generic code to be written that applies to a range of types. An example is collection classes like Vector which are independent of the types of their constituent members. Parametric polymorphism allows classes and methods to accept types as parameters, in addition to their conventional parameters, meaning that classes can be abstracted with respect to types.
is usually contrasted with AdHocPolymorphism
. Quoting ChristopherStrachey
- "Parametric polymorphism is obtained when a function works uniformly on a range of types; these types normally exhibit some common structure. Ad-hoc polymorphism is obtained when a function works, or appears to work, on several different types (which may not exhibit a common structure) and may behave in unrelated ways for each type."
Also see OnUnderstandingTypes
It should probably be pointed out that ParametricPolymorphism
as such is completely unrelated to ObjectOrientedProgramming
(unlike the suggestion created by the link above). ParametricPolymorphism
is a typical feature of all statically-typed FunctionalProgrammingLanguage
s, such as HaskellLanguage
. C++'s templates provide similar functionality, but unfortunately lack nice syntax, separate compilation and full type-safety at definition time. -- StephanHouben
in any language with first class types, but for reasons I don't understand, almost no languages have such. -- ThomasColthurst
A lot of people in the LanguagePissingMatch
have pointed out that Java doesn't have ParametricPolymorphism
. They're actually working on this as we speak. Go to the java language specification at http://java.sun.com/
(it's in there somewhere) and there should be something about planned updates. It includes assertions and ParametricPolymorphism
The JavaCommunityProcess list of all specification requests does not list ParametricPolymorphism. The closest it comes is support for generic types. Can you provide a direct URL? I suspect that ParametricPolymorphism won't make it into any JVM since it requires changes to the JLS, perhaps to bytecodes, and may not be backwards compatible.
Surely the "Generics" feature of Java 1.5 provides ParametricPolymorphism
, while CeePlusPlus
's "Templates" provides AdHocPolymorphism
? Or have I misunderstood the definitions of ParametricPolymorphism
provided on this page? -- ChrisHandley
- No, C++ templates are parameteric polymorphism; actually they can provide both. Language features in support of AdHocPolymorphism include:
- Overloading (in particular, selection of a method based on the static type of a reference; as opposed to true MultipleDispatch).
- Template specialization (though this has many useful uses in C++ besides AdHocPolymorphism)
- Type coercion.
All of which C++ can do. But templates themselves are a technique for ParametricPolymorphism
. Read the paper OnUnderstandingTypes
; it explains all of the above quite well.
See also: IncludeFileParametricPolymorphism