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

Eric Milles closed GROOVY-8459.
-------------------------------
    Resolution: Information Provided

{{Closure}}'s {{run()}} method simply delegates to {{call()}} and discards the 
return value.  Method selection prefers Runnable over Callable due to interface 
distance.  The parameter disambiguation is not considered -- GROOVY-10714 
discusses this possibility.

There is no check for return to influence method selection.  There are so many 
implicit return cases that I think it would cause more confusion if we started 
doing this.

To force the selection of "foo(Callable)" over "foo(Runnable)" you probably 
need to use a typecast and static compilation.

> Closure with return value coercion problem in overloaded methods
> ----------------------------------------------------------------
>
>                 Key: GROOVY-8459
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8459
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.13
>            Reporter: Evermind
>            Assignee: Eric Milles
>            Priority: Minor
>
> Hi, 
>     In following method overloading code, a closure with return value are 
> coerced to type Runnable, whose only method returns void. I think a compile 
> warning or error should be issued here.
> {code:groovy}
> import java.util.concurrent.Callable
> class Foo {
>     def <T> void foo(Callable<T> c) {
>         println "Callable foo"
>     }
>      void foo(Runnable r) {
>          println "Runnable foo"
>      }
>      def test() {
>         // ---> BOTH foo()s are resolved to foo(Runnable) overload version, 
> followed closures coerced to Runnable, dispite the fact that the 1st closure 
> has return value.
>          foo {
>              return 1  // Runnable's single method return type is void, 
> should not match this closure with return value.
>          }
>          foo {
>          }
>      }
>      static void main(String[] args) {
>          new Foo().test()
>      }
> }
> {code}



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

Reply via email to