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/06/11 14:55:00 UTC

[jira] [Commented] (GROOVY-11086) Wrong method is chosen when having overloaded methods with bounded type parameters

    [ https://issues.apache.org/jira/browse/GROOVY-11086?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17731361#comment-17731361 ] 

Eric Milles commented on GROOVY-11086:
--------------------------------------

Is this how Java supports method overload selection? Groovy is looking at the type of the parameter and not the return target or the type args. The list overload is preferred. You can force the {{Iterable}} method with a cast (under static compilation):
{code:groovy}
void m(Iterable it) { print "iterable" }
void m(List list) { print "list" }
@CompileStatic test() {
  m((Iterable) new LinkedList())
}
test()
{code}

> Wrong method is chosen when having overloaded methods with bounded type parameters
> ----------------------------------------------------------------------------------
>
>                 Key: GROOVY-11086
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11086
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Assignee: Eric Milles
>            Priority: Minor
>              Labels: bounded-polymorphism, overloading
>
> I have the following program
> {code}
> import java.util.*;
> class Foo<T> {}
> class Bar<T> {}
> class Main {
>   static final void test() {
>     LinkedList<Number> x = null;
>     Foo<Iterable<Number>> y = Main.<Number, Iterable<Number>>m(x);
>   }
>   static <X, T extends Iterable<X>> Foo<T> m(Iterable<X> x) {return null;}
>   static <X, T extends List<X>> Bar<T> m(List<X> x) { return null; }
> }
> {code}
> h3. Actual behaviour
> {code}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> Main.groovy: 9: [Static type checking] - Cannot assign value of type Bar<T extends java.util.List<X>> to variable of type Foo<java.lang.Iterable<java.lang.Number>>
>  @ line 9, column 31.
>        Foo<Iterable<Number>> y = Main.<Number, Iterable<Number>>m(x);
>                                  ^
> 1 error
> {code}
> h3. Expected behaviour
> Compile successfully
> In particular, the chosen method is wrong. It's not even applicable as the provided type argument (at the call-site) does not respect the upper bound of the chosen method.
> h3. Notes
> Test case adapted from the following code that uses the assertj-core lib:
> {code}
> import java.util.*;
> import org.assertj.core.api.*;
> class Foo<T> {}
> class Bar<T> {}
> class Main {
>   static final void test() {
>     LinkedList<Number> x = null;
>     FactoryBasedNavigableIterableAssert<?, Iterable<Number>, Number, ObjectAssert<Number>> res = Java6Assertions.assertThat(x, (y) -> x);
>   }
> {code}



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