Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8025290

javac implicit versus explicit lambda compilation error

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 8
    • 8
    • tools
    • b115
    • Verified

    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@...

      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              vromero Vicente Arturo Romero Zaldivar
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: