Consider the following Java function:
public void foo(Class<? extends Exception> cl, List<? extends Exception> ls) throws Exception {
ls.add(cl.newInstance());
}
This does not work, since the type captures of cl and ls are not unified and can, indeed, refer to different types. Had this function compiled, I could have called it as foo(NullPointerException.class, new List<SecurityException>()), which would have been illegal.
We can fix this, obviously, by unifying the type captures, like this:
public <T extends Exception> void foo(Class<T> cl, List<T> ls) throws Exception {
ls.add(cl.newInstance());
}
Now this function works as expected. Thus, onto my question: Is there any way to unify type captures within a single type declaration?
For example, I often find myself wanting a map that maps classes to instances of themselves. The only way I can currently think of doing this is to have an accompanying function that does unchecked casts:
private Map<Class<? extends Foo>, ? extends Foo> map = ...;
@SuppressWarnings("unchecked")
private <T extends Foo> T getFoo(Class<T> cl) {
return((T)map.get(cl));
}
But it would obviously be nicer to not have to suppress warnings and just have the compiler understand that the two type captures in the map type declaration should be the same, and then just make the map public. For example, if I could have a declaration akin to this:
<T extends Foo> Map<Class<T>, T> map = ...;
Obviously, that’s not valid syntax, but my question boils down to: Is there any valid syntax that would let my do something to that effect?
You are looking for Generics of a Higher Kind. Java does not have them and likely never will. I realize that this is a Java question, nonetheless I will point out that Scala has higher kinds. If you want to see what it means to have such power in a type system, you might want to play with it.