Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10720

Resolving wrong overloaded method?

    XMLWordPrintableJSON

Details

    Description

      I have the following program

      import java.util.Arrays;
      
      class Test {
          void test() {
            Double[] x = null;
            var y = Arrays.stream(x).map(d -> "fda").findFirst();
          }
      }
      

      Actual behavior

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      test.groovy: 7: [Static type checking] - Cannot return value of type java.lang.String for lambda expecting double
       @ line 7, column 41.
         y = Arrays.stream(x).map(d -> "fda").fin
                                       ^
      
      1 error
      

      Expected behavior

      Compile successfully

      This issue stems from the fact that there are two relevant overloaded `stream()` methods in the class `java.utils.Arrays`. See the corresponding signatures below:

      • static DoubleStream stream​(double[] array)
      • static <T> Stream<T> stream​(T[] array)

      So the compiler chooses to call the first method, because it thinks that the first method is more specific than the second one. I don't know if this is a bug or a feature. FYI, javac resolves the second method.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              theosot Thodoris Sotiropoulos
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: