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 2024/09/04 16:22:00 UTC

[jira] [Updated] (GROOVY-11446) Missing type mismatch when dealing with a polymorphic function expecting a wildcard

     [ https://issues.apache.org/jira/browse/GROOVY-11446?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Eric Milles updated GROOVY-11446:
---------------------------------
    Language: groovy

> Missing type mismatch when dealing with a polymorphic function expecting a wildcard
> -----------------------------------------------------------------------------------
>
>                 Key: GROOVY-11446
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11446
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Priority: Minor
>
> I have the following program
> {code:java}
> class Test {
>   public static void main(String[] args) {
>     List<?> d = ["fda"];
>     List<Number>  x = m(d);
>     x[0].intValue();
>   }
>   public static <T> List<T> m(List<? extends T> x) { return x }
> } {code}
> h3. Actual behavior
> The code compiles, but when running the program I receive the following exception
>  
> {code:java}
> Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
>     at Test.main(test.groovy:5) {code}
> h3. Expected behavior
> The code should have been rejected, as the return type of method `m()` is List<?>, which is incompatible with the expected type List<Number>
> h3. Notes
> Notably, when changing the signature of method m as
> {code:java}
> public static <T> List<T> m(List<T> x) { return null; } {code}
> the code is rejected as expected.



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