Ada  -- generic




generic
    type item is private;
    -- can be assigned; other characteristics are hidden
    max_items : in integer := 100;  -- max by default
package queue is
    procedure push( it : in item);
    function pop  return item;
private
    subtype index is integer range 1..max_items;
    items : array(index) of item;
    next_free, next_full : index := 1;
end queue;


package body queue is
    procedure push( it : in item) is
    begin
        items(next_free) := it;
        next_free := next_free mod max_items + 1;
    end push;

    function pop return item is
        rtn : item := items(nex_full);
    begin
        next_full := next_full mod max_items + 1;
        return rtn;
    end pop;

    :
end queue;


-- type process is previously declared


package int_queue is new queue (integer, 50);

package message_list is new queue ( process );



ADA generic:

- Sablon szerződés modell :

    - a sablon lefordítható példányosítás nélkül
    - ha a sablon lefordul, akkor garantált.
          hogy minden példányosítás, ami megfelel a specifikációnak,
          az megfelel a sablon teljes törzsének.
    - a sablon specifikációja, törzse és a példányosítások
          külön fordítási egységek, és a példányosításhoz csak a
          sablon specifikációja kell.



generic
    type Elem  is private;
    type Index is ( <> );
    type Vector is array ( Index range <> ) of Elem;
    with function "<" ( X, Y : Elem ) return Boolean is <>;
procedure Sort ( V : in out Vector );







Functional language (clean)


isort :: [a] -> [a]  | Ord a
isort []    = []
isort [a:x] = Insert a (isort x)

Insert :: a [a] -> [a]  | Ord a
Insert e [] = [e]
Insert e [x:xs]
    | e <= x    = [e,x:xs]
    | otherwise = [x:Insert e xs]







Java:


Pizza

http://pizzacompiler.sourceforge.net/
Precompiler and immediate bytecode generator.
Also pointers fo functions

Generic Java (GJ)

http://www.cis.unisa.edu.au/~pizza/gj/
http://www.research.avayalabs.com/user/wadler/gj/
http://java.sun.com/people/gbracha/generics-update.html

- Gilad Bracha and David Stoutamire (JavaSoft),
- Martin Odersky (Univ. South Australia)
- Philip Wadler (Bell Labs, Lucent)


- Support for generics,
  Vector<String> as opposed to Java class Vector
- Superset of the Java programming language
- Compiles into JVM
- Compatible with existing libraries
  Vector<String> is implemented by Vector
- Efficient translation no runtime info about types



interface Comparable
{
    public int compareTo(Object that);
}



interface Comparable<A>
{
    public int compareTo( A that);
}



class Collections2
{
    public static <A implements Comparable<A> >
    A max( Collection<A> xs)
    {
        Iterator<A> xi = xs.iterator();
        A w = xi.next();

        while ( xi.hasNext() )
        {
            A x = xi.next();
            if ( w.compareTo(x) < 0 )
                w = x;
        }
    }
}