[ https://issues.apache.org/jira/browse/GROOVY-10972 ]


    Eric Milles deleted comment on GROOVY-10972:
    --------------------------------------

was (Author: emilles):
https://github.com/apache/groovy/commit/3748eb388f2453da38ef44973ebfecf6c5ca8270

> Issue in resolving overloaded method reference
> ----------------------------------------------
>
>                 Key: GROOVY-10972
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10972
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Assignee: Eric Milles
>            Priority: Major
>
> I have the following program
> {code}
> import java.util.function.Function;
> import java.util.LinkedList;
> class Foo {}
> class Test {
>     public static void main(String[] args) {
>         LinkedList<Foo> x = new LinkedList();
>         x.add(new Foo());
>         Function<Integer, Foo> z = x::remove;
>         Foo k = z.apply(0); // ClassCastException boolean cannot be cast to 
> Foo.
>     }
> }
> {code}
> h3. Actual behavior
> The compiler accepts the code, but the following exception is triggered at 
> runtime.
> {code}
> Exception in thread "main" 
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast 
> object 'false' with class 'java.lang.Boolean' to class 'Foo'
>         at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:425)
>         at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:336)
>         at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:256)
>         at 
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
>         at Test.main(test.groovy:14)
> {code}
> There problem happens because there are two overloaded methods inside 
> LinkedList:
>   * E remove(int x)
>   * boolean remove(Object d)
> The compiler seems to resolve the second method (i.e., boolean remove 
> (Object)) although its return type does not match with Function<Integer, Foo>.
> Based only on the argument type, both methods are applicable. If the second 
> "remove" method is more specific than the first one, then a compiler error 
> should be raised at compile-time, because boolean does not match Foo. If this 
> is not the case, then the program should call the first "remove" method.



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

Reply via email to