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 2022/05/25 16:42:00 UTC

[jira] [Updated] (GROOVY-10529) Cannot use closures as arguments for varargs method

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

Eric Milles updated GROOVY-10529:
---------------------------------
    Labels: varargs  (was: )

> Cannot use closures as arguments for varargs method
> ---------------------------------------------------
>
>                 Key: GROOVY-10529
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10529
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 3.0.10
>         Environment: Edition	Windows 10 Enterprise
> Version	20H2
> Installed on	‎10/‎12/‎2020
> OS build	19042.685
> Experience	Windows Feature Experience Pack 120.2212.551.0
>            Reporter: Kevin Sheppard
>            Priority: Minor
>              Labels: varargs
>
> When I run the following code:
> {code:groovy}
> interface Handler {
>     void handle(String)
> }
> def foo(Handler... handlers) {
>     handlers.each { it.handle('Hello!') }
> }
> foo({ print(it) }, { print(it.toUpperCase()) }) {code}
> I get this error message:
> {noformat}
> No signature of method: ConsoleScript8.foo() is applicable for argument types: (ConsoleScript8$_run_closure1, ConsoleScript8$_run_closure2) values:    [ConsoleScript8$_run_closure1@4359df7, ConsoleScript8$_run_closure2@4288c46b]
> {noformat}
> Apparently, Groovy isn't seeing the closures as instances of Handler. This doesn't happen in Java or Kotlin.
> The workaround is to cast each closure as a Handler instance, but it makes for very awkward usage of the method.
> {code:java}
> foo({ print(it) } as Handler, { print(it.toUpperCase()) } as Handler)  {code}



--
This message was sent by Atlassian Jira
(v8.20.7#820007)