import java.util.*;
class Test {
static <T extends Comparable<T>> void f( List<T> l ) {}
static <E extends List<? extends Comparable<?>>> E g( E x ) {
// <T>f(java.util.List<T>) in Test cannot be applied to (E)
f(x);
return x;
}
}
my f-method computes the next permutation of the list in-place.static <E extends List<? extends Comparable<?>>> E g( E x ) {The problem is that the two ? aren't the same thing. If they're meant to be the same, make them the same:
static <T, E extends List<T extends Comparable<T>>> E g( E x ) {
min.java:6: > expected
static <T, E extends List<T[here] extends Comparable<T>>> E g( E x )
min.java:6: <identifier> expected
static <T, E extends List<T extends Comparable<T>[here]>> E g( E x )
And with
static <T, E extends List<? extends Comparable<T>>> E g( E x )
there is no syntax error, but the same as in the original post.static <T extends Comparable<T>, E extends List<T>> E g( E x )
Although, when I try to use the same definition in a class:
class Iter<T extends Comparable<T>, E extends List<T>> implements Iterator<E>
T is not inferred, I have to use
(new Iter<Integer, ArrayList<Integer>>(a)).next(); // works
(new Iter<ArrayList<Integer>>(a)).next(); // wrong number of type arguments
(new Iter(a)).next(); // unchecked
Is there a way to infer T? In theory, it should be possible, since we know E extends List<T>, and it works for functions... But this definition works:I'm sure that's what ejp meant to write, that's the correct solution.static <T extends Comparable<T>, E extends List<T>> E g( E x )
Is there a way to infer T? In theory, it should be possible, since we know E extends List<T>, and it works for functions...Java's type inference is rather conservative. As I understand it there was at least one previous algorithm which proved to be unsound, so I wager they wanted to err on the side of caution.
import java.util.*;
class Test {
static <T extends Comparable<T>> void f( List<T> l ) {}
static <E extends Comparable<E>> List<E> g( List<E> x ) {
f(x);
return x;
}
}
The problem is that, as it has been said, the following types: