Generics in other languages

You can create generic constructions in other languages: in ADA, Eiffel and Java (since 1.5)

ADA generic

We can create an abstract queue type with generic parameters


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;

package int_queue is new queue (integer, 50);

package message_list is new queue ( process );

Ada supports dynamic arrays: max_items may not be known until runtime in C++ there are restrictions on scalar template parameters

Generic program is a unit of abstraction - its interface should provide all the information that must be known to use The operations on a generic parameter type should be explicitly declared

In a more complex case we must specify the neccessary operations by means of a trailing with clause


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 languages

Most functional language can handle generics. (This is an example from 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]

Generic in Java

The long-waited generics are here in JAVA 1.5. Previously it was implemented in PIZZA and in GJ (Generic JAVA).

http://pizzacompiler.sourceforge.net

Precompiler and immediate bytecode generator. Also pointers fo functions

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)

In May 2001 Sun releases prototype for Generics in Java It was planned to introduce GJ-like generics into Java 1.4, but this was withdrown. January 2003: Generics headed for inclusion in Java 1.5

  • 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;
        }
    }
}

class MyType implements Comparable<MyType>
{
    // ...
    public int compareTo(MyType) { ... }
}

LinkedList<MyType> ml = new LinkedList<MyType>();
// ...
MyType mmax = Collections2.max(ml);     // not max<MyType>(ml)