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

Type inference yields a wrong type even if the type of the associated variable is given

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • None
    • None
    • Static Type Checker
    • None

    Description

      The following code demonstrates a limitation of type inference in a real-world example.
      I have the following program

      import java.util.*;
      import java.util.stream.*;
      
      class Test {
          void test() {
            Stream<Number> x = Arrays.stream(new Integer[]{1}).flatMap((y) ->
              Arrays.stream(new Long[]{(long) 1}));
          }
      }
      

      Actual behavior

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      test.groovy: 6: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.stream.Stream<java.lang.Long> to: java.util.stream.Stream<java.lang.Number>
       @ line 6, column 26.
               Stream<Number> x = Arrays.stream(new Integer[]{1}).flatMap((y) -> Arrays.stream(new Long[]{(long) 1}));
                                  ^
      
      1 error
      

      Expected behavior

      Compile successfully

      The code should pass because the signature of the "flatMap" function is "<R> Stream<R> flatMap​(Function<? super T,​? extends Stream<? extends R>> mapper)". And therefore, Function<Integer, Stream<Long>> is a subtype of Function<? super Integer, ? extends Stream<? extends Number>.

      Tested against master (commit: 5a5726342adeb37a6fbaa8cdcbe2d47dee8dc56c)

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated: