Ad-hoc polymorphism
From HaskellWiki
(Difference between revisions)
(Hawiki conversion) |
m (add 'or overloading') |
||
| Line 1: | Line 1: | ||
[[Category:Glossary]] | [[Category:Glossary]] | ||
| - | A value is [[polymorphism|polymorphic]] if, depending on its context, it can assume more than one type. If the possible types are limited and must be individually specified before use, this is called ad-hoc polymorphism. | + | A value is [[polymorphism|polymorphic]] if, depending on its context, it can assume more than one type. If the possible types are limited and must be individually specified before use, this is called ad-hoc polymorphism (or overloading). |
In object-oriented languages, ad-hoc polymorphism is often called ''overloading.'' For instance, in C++, the operator '''<hask>+</hask>''' may have type (in Haskell notation) <hask>Int -> Int -> Int</hask> or <hask>String -> String -> String</hask>, and may be applied to new types provided you write a definition for it using those types as arguments. The range of possible types of '''<hask>+</hask>''' in C++ is limited to those built-in and those explicitly defined. | In object-oriented languages, ad-hoc polymorphism is often called ''overloading.'' For instance, in C++, the operator '''<hask>+</hask>''' may have type (in Haskell notation) <hask>Int -> Int -> Int</hask> or <hask>String -> String -> String</hask>, and may be applied to new types provided you write a definition for it using those types as arguments. The range of possible types of '''<hask>+</hask>''' in C++ is limited to those built-in and those explicitly defined. | ||
Revision as of 07:20, 6 October 2009
A value is polymorphic if, depending on its context, it can assume more than one type. If the possible types are limited and must be individually specified before use, this is called ad-hoc polymorphism (or overloading).
In object-oriented languages, ad-hoc polymorphism is often called overloading. For instance, in C++, the operator+
Int -> Int -> Int
String -> String -> String
+
+
Int -> Int -> Int
Float -> Float -> Float
Double -> Int -> Double
Ptr -> Int -> Ptr
+
show
Int -> String
Float -> String
(Maybe Int, String, Either Char Float) -> String
show
show
Show
show :: Show a => a -> String
length :: [a] -> Int
length
[a] -> Int
