Difference between revisions of "Ad-hoc polymorphism"

From HaskellWiki
Jump to navigation Jump to search
(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 + may have type (in Haskell notation) Int -> Int -> Int or String -> String -> String, 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 + in C++ is limited to those built-in and those explicitly defined.

Many non-object-oriented languages use ad-hoc polymorphism too. In C, the operator + may have the types Int -> Int -> Int or Float -> Float -> Float or Double -> Int -> Double or Ptr -> Int -> Ptr. The range of possible types of + in C is limited to those built-in.

In Haskell, ad-hoc polymorphism is achieved by type classes. For instance, the function show may have type of the form Int -> String or Float -> String or (Maybe Int, String, Either Char Float) -> String depending on its context, but the possible types of show are limited. The user must specify the type show will take by defining the type of its first argument as an instance of Show. (This is reflected in its signature show :: Show a => a -> String.)

Contrast ad-hoc polymorphism with parametric polymorphism of the function length :: [a] -> Int. length may be applied to lists of any type--that is, may take on an unlimited number of types (of the form [a] -> Int)--and the user may apply it to any list without needing to specify the type beforehand.