You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/04/15 16:04:00 UTC
[jira] [Updated] (GROOVY-10913) Type inference yields a wrong type even if the type of the associated variable is given
[ https://issues.apache.org/jira/browse/GROOVY-10913?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles updated GROOVY-10913:
---------------------------------
Priority: Minor (was: Major)
> 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
> Assignee: Eric Milles
> Priority: Minor
>
> 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)