Details
-
Bug
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
None
-
None
-
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)