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

Type inference not working in combination with method reference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • 21
    • None
    • tools
    • None

      The code below generates a type inference error that can only be resolved by making the diamond operator explicit (same as the selectedCellsMap field). When the method reference is changed to a lambda expression, the problem disappears.

      =======================

      package test;

      public class Problem1 {

          private static class TreeTablePosition<S,T> extends TablePositionBase {
          }

          private static abstract class TablePositionBase {
          }

          interface ListChangeListener<E> {
              public abstract static class Change<E> {
          }

          public void onChanged(Change<? extends E> c);
          }


          private static abstract class SelectedCellsMap<T extends TablePositionBase> {
              public SelectedCellsMap(final ListChangeListener<T> listener) {
              }
          }

          public static class TreeTableView {
              static class TreeTableViewArrayListSelectionModel<S> {
                  final SelectedCellsMap<TreeTablePosition<S, ?>> selectedCellsMap;

                  public TreeTableViewArrayListSelectionModel() {
                      selectedCellsMap = new SelectedCellsMap<>(this::fireCustomSelectedCellsListChangeEvent) {};
                      // this works
                      // selectedCellsMap = new SelectedCellsMap<>(c -> {}) {};
                      // this works too
                      //selectedCellsMap = new SelectedCellsMap<TreeTablePosition<S, ?>>(this::fireCustomSelectedCellsListChangeEvent) {};
                  }

                  void fireCustomSelectedCellsListChangeEvent(ListChangeListener.Change<? extends TreeTablePosition<S, ?>> c) {}
              }
          }
      }

      =======================

      I get the following error:

      $ javac test/Problem1.java -Xdiags:verbose
      test\Problem1.java:42: error: cannot infer type arguments for SelectedCellsMap<>
                selectedCellsMap = new SelectedCellsMap<>(this::fireCustomSelectedCellsListChangeEvent) {
                                   ^
        reason: cannot infer type-variable(s) T
          (argument mismatch; invalid method reference
            method fireCustomSelectedCellsListChangeEvent in class TreeTableViewArrayListSelectionModel<S> cannot be applied to given types
              required: Change<? extends TreeTablePosition<S,?>>
              found: Change<? extends T>
              reason: argument mismatch; Change<CAP#1> cannot be converted to Change<? extends TreeTablePosition<S,?>>)
        where T,S are type-variables:
          T extends TablePositionBase declared in class SelectedCellsMap
          S extends Object declared in class TreeTableViewArrayListSelectionModel
        where CAP#1 is a fresh type-variable:
          CAP#1 extends T from capture of ? extends T
      1 error

      =======================

      Expected:

      Type inference should correctly infer that T of SelectedCellsMap is TreeTablePosition<S, ?>.

      More information:

      The Eclipse Compiler accepts this without problem.

            vromero Vicente Arturo Romero Zaldivar
            jhendrikx John Hendrikx
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: