Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to avoid repetition of generic type parameters when one type could be inferred from the other?

Tags:

java

generics

This is a contrived example, but simpler to explain than my actual code:

public interface ContainerOwner<T, C extends Container<T>> {
    // ...
}

I'd like to avoid the repetition of T in that type signature because it becomes unwieldy when the parameters themselves have parameters, for example:

ContainerOwner<
     Optional<Future<Map<String, Integer>>>,
     List<Optional<Future<Map<String, Integer>>>>
> foo;

In this example, I feel like the first parameter could be inferred from the second. Is there a trick to do that?

like image 379
Peter Hall Avatar asked Oct 30 '22 15:10

Peter Hall


1 Answers

One way could be to use a more specific sub-interface that will need only one type parameter, something like:

public interface ListOwner<T> extends ContainerOwner<T, List<T>> {
}

Then your code would be:

ListOwner<Optional<Future<Map<String, Integer>>>> foo;
like image 101
Nicolas Filotto Avatar answered Nov 09 '22 13:11

Nicolas Filotto