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

reference to METHOD is ambiguous

XMLWordPrintable

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

      I am getting both method register(Runnable) in TEST and method register(Func) in TEST match error when no such error should ever be generated.

      The reproducer:

      ```
      public class TEST {
          @FunctionalInterface
          public static interface Func {
              public boolean accept();
          }
          
          protected void register(Runnable r) { }
          
          protected void register(Func f) { }
          
          void test() {
              register(this::run); // "This method is ambiguous for the type TEST"
          }
          
          // commenting out this line resolves the "ambiguous" error
          // why does java think this method is appropriate for the lambda above??
          public boolean run(String s) { return false; }

          // commenting out this line adds another error
          public void run() { }
      }
      ```

      The Error (using javac 23.0.1):

      $ javac TEST.java
      TEST.java:14: error: reference to register is ambiguous
              register(this::run); // "This method is ambiguous for the type TEST"
              ^
        both method register(Runnable) in TEST and method register(Func) in TEST match
      1 error

      I don't understand how public boolean run(String s) method can match any of the register() methods.

      Please correct me if I am wrong.


      REFERENCES

      [0] https://github.com/andy-goryachev-oracle/Test/blob/main/doc/Lambda/LambdaError.md

        1. TEST.java
          0.6 kB
          Andy Goryachev

            Unassigned Unassigned
            angorya Andy Goryachev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: