Home
Main website
Display Sidebar
Hide Ads
Recent Changes
View Source:
PolymorphicTypes
Edit
PageHistory
Diff
Info
LikePages
[ProgrammingLanguage]s with PolymorphicTypes allow you to write great range of generic functions, yet preserve StrongTypeChecking. With polymorphic typing you don't have to specify the exact type of everything in your program, you may use blank ''type variables''. This function type in [OCaml] is not polymorphic: __val__ zip_ints : int list -> int list -> (int * int) list That says __zip_ints__ will take two lists of integers and return a list of pairs of integers. It's useful, but only for one thing. This function in OCaml is polymorphic: __val__ zip : 'a list -> 'b list -> ('a * 'b) list This __zip__ takes two lists of items of types __'a__ and __'b__ (which may or may not be the same type) and returns a list of __'a__ and __'b__ pairs. When the compiler sees this function call: zip [[1; 2; 3] [["orange"; "apple"; "banana"] It will know that the result should be a list of integer/string pairs and type check the rest of the program accordingly. [ML] and [Haskell] have polymorphic typing. In [C] you can approximate polymophic typing with __void__ pointer arguments, which will take pointers to any type. In [Java] and [C++] approximate polymorphic typing by using Object classes, which all objects belong to. But in both cases you lose strong type checking, so the quality of your code suffers. ''It should be noted here that this page is talking about what is sometimes known as __parametric polymorphism__. [C++] has templates, which satisfy the definition above, but are known to be syntactic; each instantiation creates a specialised version of the code. In a "true polymorphic system" only one version of the generated code would be used. There are other ways to have polymorphic types, they often come up in ObjectOrientation. Oh, and parametric polymorphism is also often referred to as generics. -- SamJansen'' ''Beginning with version 1.5 of the [Java] SDK, [Java] now also supports the concept of generics. -- DavidHallett'' !!!Java Syntax __Paramaterized Type__ Vector<String> stringVector = new Vector<String> List<Integer> integerList = new List<Integer> __Interface__ interface List<Element> implements !MyInterface{...} __Class__ class !MyList<Element> {...} class !MyList<Element> implements List<Element> {...} __Method__ boolean containsBoth(Element a, Element b); static <Element> boolean swap(List<Element> list, int i, int j); [Java Generics on developer.java.sun.com | http://developer.java.sun.com/developer/technicalArticles/releases/generics/] !!!An Example This is Glyn defining a binary tree type on his [OCaml] interpreter: # __type__ 'a tree = Leaf __of__ 'a | Node __of__ 'a tree * 'a tree ;; ''type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree'' (That's Glyn typing at the [OCaml] interpreter prompt and [OCaml] responding.) The type __tree__ is polymorphic: __'a__ is a place holder for any type. For example you might want a __string tree__: # __let__ my_tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat") ;; ''val my_tree : string tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat")'' The [OCaml] interpreter worked that was a string tree for itself. From his tree type he can make __int trees__, __char trees__, __string tree trees__, whatever. These would all be incompatible types, because __ints__, __chars__ and __strings__ are incompatible. However, functions he writes for __'a tree__s can be used on any type of tree: # __let rec__ count_leaves (t : 'a tree) : int = __match__ t __with__ | Leaf _ -> 1 | Node (left,right) -> count_leaves left + count_leaves right ;; ''val count_leaves : 'a tree -> int = <fun>'' # count_leaves my_tree ;; ''- : int = 3'' # count_leaves (Node (Leaf 1, Leaf 2)) ;; ''- : int = 2''
5 pages link to
PolymorphicTypes
:
JavaAndC++
OCaml
HigherOrderFunctions
Haskell
ML