Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can captures in Java generics be unified in type declarations?

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?

like image 717
Dolda2000 Avatar asked Feb 08 '12 21:02

Dolda2000


People also ask

Which of the following statement is incorrect about generics?

Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java? Explanation: None.

Which of the following Cannot be type parameterized?

6. Which of these Exception handlers cannot be type parameterized? Explanation: we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.

Can generics take multiple type parameters?

A Generic class can have muliple type parameters.


1 Answers

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.

like image 110
Ben Schulz Avatar answered Sep 23 '22 14:09

Ben Schulz