Is there a way to avoid repeating type type parameters when one type can be inferred from another?

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

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

I would like to avoid repeating T in a signature of this type, because it becomes cumbersome when the parameters themselves have parameters, for example:

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

In this example, it seems to me that the first parameter can be inferred from the second. Is there any trick for this?

+5
source share
1 answer

One way could be to use a more specific sub-interface, for which only one parameter is required, for example:

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

Then your code will look like this:

 ListOwner<Optional<Future<Map<String, Integer>>>> foo; 
+2
source

Source: https://habr.com/ru/post/1261431/


All Articles