Difference between revisions of "Polymorphism"
Jump to navigation
Jump to search
BrettGiles (talk | contribs) m (Make link pages) |
(Improve wording, and add Wikipedia link.) |
||
Line 1: | Line 1: | ||
[[Category:Glossary]] |
[[Category:Glossary]] |
||
− | A value is |
+ | A value is polymorphic if, depending on the context where it's used, it can take on more than one type. |
− | + | There are different kinds of polymorphism. |
|
#[[Parametric polymorphism]]; mostly found in functional languages |
#[[Parametric polymorphism]]; mostly found in functional languages |
||
⚫ | |||
#[[Inclusion polymorphism]]; mostly found in object oriented languages |
#[[Inclusion polymorphism]]; mostly found in object oriented languages |
||
⚫ | |||
− | == |
+ | == Examples == |
<haskell> |
<haskell> |
||
foldr :: forall a b. (a -> b -> b) -> b -> [a] -> b |
foldr :: forall a b. (a -> b -> b) -> b -> [a] -> b |
||
</haskell> |
</haskell> |
||
− | <hask>foldr</hask> is a |
+ | <hask>foldr</hask> is a parametric polymorphic [[function]]. When actually used, it may take on any of a variety of types, for example: |
<haskell> |
<haskell> |
||
::(Char ->Int->Int)->Int->String->Int |
::(Char ->Int->Int)->Int->String->Int |
||
::(String->String->String)->String->[String]->String |
::(String->String->String)->String->[String]->String |
||
</haskell> |
</haskell> |
||
− | An "integer literal" is polymorphic: |
+ | An "integer literal" is a parametric polymorphic data type: |
<haskell> |
<haskell> |
||
1 :: forall t. (Num t) => t |
1 :: forall t. (Num t) => t |
||
Line 24: | Line 24: | ||
== References == |
== References == |
||
*[http://citeseer.nj.nec.com/cardelli85understanding.html On Understanding Types, Data Abstraction, and Polymorphism (1985)], by Luca Cardelli, Peter Wegner in ACM Computing Surveys. |
*[http://citeseer.nj.nec.com/cardelli85understanding.html On Understanding Types, Data Abstraction, and Polymorphism (1985)], by Luca Cardelli, Peter Wegner in ACM Computing Surveys. |
||
+ | |||
+ | *[http://en.wikipedia.org/wiki/Type_polymorphism Type polymorphism] at Wikipedia |
Revision as of 07:02, 6 October 2009
A value is polymorphic if, depending on the context where it's used, it can take on more than one type.
There are different kinds of polymorphism.
- Parametric polymorphism; mostly found in functional languages
- Ad-hoc polymorphism or overloading
- Inclusion polymorphism; mostly found in object oriented languages
Examples
foldr :: forall a b. (a -> b -> b) -> b -> [a] -> b
foldr
is a parametric polymorphic function. When actually used, it may take on any of a variety of types, for example:
::(Char ->Int->Int)->Int->String->Int
::(String->String->String)->String->[String]->String
An "integer literal" is a parametric polymorphic data type:
1 :: forall t. (Num t) => t
References
- On Understanding Types, Data Abstraction, and Polymorphism (1985), by Luca Cardelli, Peter Wegner in ACM Computing Surveys.
- Type polymorphism at Wikipedia