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

Inconsistent ambiguous warning for overloaded methods with functional interfaces

XMLWordPrintable

    • x86
    • windows_8

      FULL PRODUCT VERSION :
      java version "1.8.0"
      Java(TM) SE Runtime Environment (build 1.8.0-b132)
      Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [version: 6.0.6002]

      A DESCRIPTION OF THE PROBLEM :
      The following two methods are reported potentially ambigous:

      - Object call1(Function<Object, Object> function)
      - void call1(Consumer<Object> consumer)

      However, the following two are not:

      - void call2(Runnable consumer)
      - Object call2(Callable<Object> function)

      This seems inconsitent because in both case the argument list is the same for the functional interfaces and the overloaded methods differ only by one of the functional interfaces having a return value while the other does not.

      When calling them,

      call1((Object arg) -> {})
      call2(() -> {})

      compiles without any problem (no warning). However,

      call1((arg) -> {})

      is reported ambigous and does not compile.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the example code to see the warning.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected no warnings and also, I expect "call1((arg) -> {})" to compile.
      ACTUAL -
      Warning is printed for the method "call1" and "call1((arg) -> {})" does not compile (due to reported being ambiguous).

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      warning: [overloads] call1(Consumer<Object>) in ExampleMethods is potentially ambiguous with call1(Function<Object,Object>) in ExampleMethods

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.concurrent.Callable;
      import java.util.function.Consumer;
      import java.util.function.Function;

      public final class ExampleMethods {
          public static void call1(Consumer<Object> consumer) {
              consumer.accept(null);
          }

          public static Object call1(Function<Object, Object> function) {
              return function.apply(null);
          }

          public static void call2(Runnable consumer) {
              consumer.run();
          }

          public static Object call2(Callable<Object> function) throws Exception {
              return function.call();
          }

          public static void call1Example() {
              call1((Object arg) -> {});
              // Uncommenting the line below is a compile time error.
              // call1((arg) -> {});
          }

          public static void call2Example() {
              call2(() -> {});
          }
      }
      ---------- END SOURCE ----------

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: