Please help improve this article by adding citations to reliable sources. This section needs additional citations for verification. Thus, ad hoc polymorphism can generally only support a limited number of such distinct types, since a separate implementation has to be provided for each type. (Note that since there is only one type variable, the function cannot be applied to just any pair of lists: the pair, as well as the result list, must consist of the same type of elements.) For each place where append is applied, a value is decided for a.įollowing Christopher Strachey, parametric polymorphism may be contrasted with ad hoc polymorphism, in which a single polymorphic function can have a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. We say that the type of append is parameterized by a for all values of a. Where denotes the type of lists with elements of type a. Let the type variable a denote the type of elements in the lists. Such functions and data types are called generic functions and generic datatypes respectively and form the basis of generic programming.įor example, a function append that joins two lists can be constructed so that it does not care about the type of elements: it can append lists of integers, lists of real numbers, lists of strings, and so on. Using parametric polymorphism, a function or a data type can be written generically so that it can handle values identically without depending on their type. In programming languages and type theory, parametric polymorphism is a way to make a language more expressive, while still maintaining full static type-safety.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |