# What is the use case of multi-type-parameters generic interface?

Previously, I ask a similar question, but the answer given only demonstrates the usage of multiple-type-parameters(MTP) data structure, but not MTP generic interface.

Based on my experience, generic interface is useful for generalizing algorithms. For example, the sorting algorithm can be defined as such:

// Java code

interface Comparable<T> {
Boolean compareTo(T other)
}

void <T extends Comparable<T>> sort(List<T> elements) {
// body
}


So, the code above demonstrated the usage of single-type-parameter generic interface.

However, I have not encountered any use case for multiple-type-parameter generic interface up until now.

Thus, I am hoping that some one could provide me some use cases regarding multiple-type-parameter interfaces.

Also, if possible, please explain how multiple-type-parameter could improve some situation.

(For example, the Comparable interface allows the sort function to be generalized, so that one do not need to define multiple sort function for different types of element)

P/S: I ask this question because I am considering whether to include MTP generic interface in my new programming language.

A map, also known as a dictionary, is a datastructure that maps keys of type K to values of type V. It is naturally parameterized by two parameters, K and V. This is the case in the Java collections library, for instance, where you can find the interface Map<K,V>.

There are may other examples, of course. For instance, you could have a graph whose vertices are labeled with type A and edges labeled with type B. Then it would be natural to have a class Graph<A,B>.

Supplemental: The OP is still asking for examples where generics with several parameters are used. In order to prod the imagination, here are some:

1. Dijsktra's algorithm for finding shortest paths in a graph could be parametrized by Graph<A, B extends Comparable> so that it works on any graph whose edges are labeled by values that implement the Comparable interface. (Yes, A is not used in this example, but that is beside the point. It is useful to allow vertices of graphs to be labeled.)

2. An implementation of Map<K, V> using AVL trees would be a class AVLMap<K extends Comparable, V>. Such a class will contain generic implementations of algorithms for manipulation of AVL trees. Other variants are possible (red-black trees, splay trees, etc.), each leading to a whole family of algorithms.

3. A function which takes a Map<K,V> and determines whether a given map represents an injective function.

4. A function which takes a Map<K,List<K>> representing a graph by the adjacency lists and determines whether the graph is bipartite. Or counts the number of cycles. Or computes the chromatic number. This is an example where there is a dependency between the parameters of Map. If we did not have multi-parameter interfaces, then we could not even express dependencies between the parameters.

5. A function which takes a Map<K, V> and converts it to a HasMap<K, V>.

6. A function whic takes a Map<K, V> and computes the inverse map Map<V, Set<K>>.

• thanks for the answer, but I think you misunderstood my questions, because I was asking for generic interface not generic class. – Wong Jia Hau Aug 26 '18 at 11:40
• I misspoke. But the example I linked to is an interface. Did you even care to click. On the link? Additionally I would expect that you would make the effort to understand the ideas expressed in the answers. – Andrej Bauer Aug 26 '18 at 13:24
• It was indeed an interface, but which algorithm would take a generic map? If you could give me some example, then this would be the tie-breaker for my decision of whether to include multiple-type-parameter interface for my programming language. – Wong Jia Hau Aug 26 '18 at 13:49
• I would advise using imagination at this point. You want everything served on a silver platter. – Andrej Bauer Aug 26 '18 at 14:09
• I added some examples. Please note that very generic interfaces will in general support only very generic functions. But this does not mean that such interfaces are useless. On the contrary, it is very useful to be able to implement simple generic functions (I mention some above) once and for all. – Andrej Bauer Aug 26 '18 at 18:13

The most typical usecase I've encountered is generic idea of container, parameterized by both the container type and contained type. F.e. you can describe generic type class of containers providing get/set/delete operations for arbitrary types providing hash() operation.

Even more complicated usecase is type+container+monad, involving concrete monad where all update operations on the container occur, but this is mainly interesting in the Haskell realm.

• Do you mind to provide a code example? It can be Haskell or Java or any language you like. – Wong Jia Hau Aug 26 '18 at 6:06