Description
This code:
import java.util.ArrayList;
import java.util.List;
import static java.util.Comparator.comparing;
import static java.util.Comparator.nullsFirst;
public class Test<T>
{
private void Test()
{
List<String> list = new ArrayList<>();
list.sort(nullsFirst(comparing((String e) -> e.trim())));
}
}
fails with:
Test.java:11: error: incompatible types: cannot infer type-variable(s) T,U
list.sort(nullsFirst(comparing((String e) -> e.trim())));
^
(argument mismatch; Function<String,CAP#1> cannot be converted to Function<? super Object,? extends String>)
where T,U are type-variables:
T extends Object declared in method <T,U>comparing(Function<? super T,? extends U>)
U extends Comparable<? super U> declared in method <T,U>comparing(Function<? super T,? extends U>)
where CAP#1 is a fresh type-variable:
CAP#1 extends String from capture of ? extends String
1 error
but if the lambda is implicit: e -> e.trim(), it compiles just fine.
Reported by Zhong Yu through lambda-dev@...
import java.util.ArrayList;
import java.util.List;
import static java.util.Comparator.comparing;
import static java.util.Comparator.nullsFirst;
public class Test<T>
{
private void Test()
{
List<String> list = new ArrayList<>();
list.sort(nullsFirst(comparing((String e) -> e.trim())));
}
}
fails with:
Test.java:11: error: incompatible types: cannot infer type-variable(s) T,U
list.sort(nullsFirst(comparing((String e) -> e.trim())));
^
(argument mismatch; Function<String,CAP#1> cannot be converted to Function<? super Object,? extends String>)
where T,U are type-variables:
T extends Object declared in method <T,U>comparing(Function<? super T,? extends U>)
U extends Comparable<? super U> declared in method <T,U>comparing(Function<? super T,? extends U>)
where CAP#1 is a fresh type-variable:
CAP#1 extends String from capture of ? extends String
1 error
but if the lambda is implicit: e -> e.trim(), it compiles just fine.
Reported by Zhong Yu through lambda-dev@...
Attachments
Issue Links
- duplicates
-
JDK-8025138 Type inference error when reversing a comparator
- Resolved