CSC448: Type Checking Revisited: Generic Polymorphism II [112/133] Previous pageContentsNext page

This feature of a language is known as generic or parametric polymorphism, but it should be distinguished from the subtype polymorphism found in almost all object-oriented languages.

How does this fragment of Java code:

  List reverse1 (List l)
  {
    List result = new ArrayList ();
    Iterator iter = l.iterator ();
    while (iter.hasNext ()) {
      Object o = iter.next ();
      result.add (o);
    }
    return result;
  }
        

differ from this ML code?

> val 'a reverse = fn : 'a list -> 'a list

fun reverse [] = []
|   reverse (x::xs) = (reverse xs) @ [x]; 
        

Previous pageContentsNext page