ProgrammingLanguages 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.
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
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
type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree
val my_tree : string tree = Node (Node (Leaf "Cow", Leaf "Pig"), Leaf "Cat")
val count_leaves : 'a tree -> int = <fun>
- : int = 3
- : int = 2
5 pages link to PolymorphicTypes: