Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Polymorphism (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Parametric polymorphism=== {{Further|Parametric polymorphism}} ''Parametric polymorphism'' allows a function or a data type to be written generically, so that it can handle values ''uniformly'' without depending on their type.<ref name="bjpierce">{{cite book |last1=Pierce |first1=B.C. |date=2002 |chapter=23.2 Varieties of Polymorphism |chapter-url=https://books.google.com/books?id=ti6zoAC9Ph8C&pg=PA340 |title=Types and Programming Languages |publisher=MIT Press |isbn= 9780262162098 |pages=340β1 |url=}}</ref> Parametric polymorphism is a way to make a language more expressive while still maintaining full static [[type safety]]. The concept of parametric polymorphism applies to both [[data type]]s and [[Function (computer programming)|functions]]. A function that can evaluate to or be applied to values of different types is known as a ''polymorphic function.'' A data type that can appear to be of a generalized type (e.g., a [[List (abstract data type)|list]] with elements of arbitrary type) is designated ''polymorphic data type'' like the generalized type from which such specializations are made. Parametric polymorphism is ubiquitous in functional programming, where it is often simply referred to as "polymorphism". The next example in [[Haskell]] shows a parameterized list data type and two parametrically polymorphic functions on them: <syntaxhighlight lang="Haskell"> data List a = Nil | Cons a (List a) length :: List a -> Integer length Nil = 0 length (Cons x xs) = 1 + length xs map :: (a -> b) -> List a -> List b map f Nil = Nil map f (Cons x xs) = Cons (f x) (map f xs) </syntaxhighlight> Parametric polymorphism is also available in several object-oriented languages. For instance, [[Template (C++)|templates]] in [[C++]] and [[D (programming language)|D]], or under the name [[Generics in Java|generics]] in [[C Sharp (programming language)|C#]], [[Delphi (software)|Delphi]], Java, and [[Go (programming language)|Go]]: <syntaxhighlight lang="CSharp"> class List<T> { class Node<T> { T elem; Node<T> next; } Node<T> head; int length() { ... } } List<B> map(Func<A, B> f, List<A> xs) { ... } </syntaxhighlight> [[John C. Reynolds]] (and later [[Jean-Yves Girard]]) formally developed this notion of polymorphism as an extension to lambda calculus (called the polymorphic lambda calculus or [[System F]]). Any parametrically polymorphic function is necessarily restricted in what it can do, working on the shape of the data instead of its value, leading to the concept of [[parametricity]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)