Thodoris Sotiropoulos created GROOVY-10913:
----------------------------------------------

             Summary: Type inference yields a wrong type even if the type of 
the associated variable is given
                 Key: GROOVY-10913
                 URL: https://issues.apache.org/jira/browse/GROOVY-10913
             Project: Groovy
          Issue Type: Bug
          Components: Static Type Checker
            Reporter: Thodoris Sotiropoulos


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

{code}
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}));
    }
}
{code}

h3. Actual behavior

{code}
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
{code}

h3. 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)




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to