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/08 18:34:00 UTC

[jira] [Commented] (GROOVY-10597) STC: support spread arguments for varargs

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

Eric Milles commented on GROOVY-10597:
--------------------------------------

https://github.com/apache/groovy/commit/e7527901444e08c5ea1ea6443385033e051c4c14

> STC: support spread arguments for varargs
> -----------------------------------------
>
>                 Key: GROOVY-10597
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10597
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Static Type Checker
>    Affects Versions: 4.0.2
>            Reporter: Christopher Smith
>            Assignee: Eric Milles
>            Priority: Minor
>
> I have a varargs method that takes many parameters for inclusion into an output JSON array. I am needing to preprocess some of these parameters, and I tried to use the spread operator to deconstruct them inline. However, I'm receiving the error "The spread operator cannot be used" because the compiler can't determine arity.
> This is perfectly sensible, but in the specific case where the spread operator would be applied in a position where its values expand into a varargs, as long as the element type produced by the spread is a subtype of the declared varargs type, it would be helpful for the spread to succeed.
> Sample:
> {code:groovy}
> @CompileStatic
> class Groovy10597 {
>   static void output(int code, String... widgets) {
>     println "$code: ${Arrays.toString(widgets)}"
>   }
>   static List<String> createWidgets() {
>     ['w1', 'w2']
>   }
>   static void sample() {
>     output(42, 'w0', *createWidgets(), 'w3')
>   } 
> }
> {code}



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