In Java it is tedious to write:
Pair<String, String> pair = new Pair<String, String>("one", "two");
It would be nice if the types were inferred for you so that you can at least do this:
Pair<String, String> pair = new Pair("one", "two");
And again skip the general options.
You can create a static method that can spoof it like this:
public static <T, S> Pair<T, S> new_(T one, S two) {
return new Pair<T, S>(one, two);
}
And then use it as: Pair.new_("one", "two").
Is it possible to build type inference in the constructor to avoid hacking?
I was thinking of something like:
public <S,T> Pair(S one, T two) {
this.one = one;
this.two = two;
}
But then you come across generic types of collisions. Anyone have any thoughts?
source
share