So, I'm developing a Lisp, which means I have to support untyped, dynamically type-checked code in any case. But I still like typeful programming.
Generics, or parametric polymorphism, are jolly useful, even on a totally limited scale: all I want from the first round of this endeavor is to support something like the following:
(defclass (list T))
(defmethod add ((list (list T)) (element T))
Here, I define the class (list T) - a list parameterized over the type of its elements, T. That's the same as Java's List<T>. The method ADD takes such a list and an element, and adds it to the list.
To create an instance of a parameterized class, you have to pass a concrete type argument to use for the element type, as in:
(defvar *my-list* (make (list string)))
This creates a list that accepts strings (and instances of subclasses of string) as elements.
The instance has to remember that it's a list of strings for type safety. When ADD is called on a list, the runtime needs to check that the element type matches:
(add *my-list* "foo") ;; OK
(add *my-list* 12) ;; runtime error
One more thing I'd also like is the following;
(defclass (foo (<: X bar)))
That's a class foo that has one parameter, that must be a subtype of bar, analogous to Java's Foo<X extends Bar>. Being able to give such a bound to a type parameter seems quite essential.
Furthermore, it has to be possible to pass on type parameters to superclasses, as in:
(defclass (super-1 T))
(defclass (super-2 T))
(defclass (klass X Y) ((super-1 X) (super-2 Y)))
(klass X Y) is parameterized over two types, X and Y, and passes them on to its two superclasses, each of which is parameterized over one type.
Of course, it also has to be possible to remove parameterization, as in:
(defclass string-list ((list string)))
This defines an unparameterized class string-list, which is a subclass of (list string).
Another requirement is that it has to be possible to create instances of type arguments, as in:
(defclass (klass T))
(defmethod make-a-new-t ((k (klass T)))
That's just cute, and may have some applications to dependency injection.
I'd also like to be able to write polymorphic functions:
(defun identity ((a T) -> T) a)
The arrow indicates the result type of the function. In this case it takes an instance of any type and returns it.
It should also be possible to give slots (member variables) of classes the types of type parameters:
(defclass (klass X Y) ()
This defines a class with two type parameters X and Y, (no superclasses,) and two slots slot-1 and slot-2 with the types X and Y, respectively.
These are the basic requirements. Now I need a plan! ;)