Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10597

STC: support spread arguments for varargs

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 4.0.2
    • 5.0.0-alpha-1
    • Static Type Checker
    • None

    Description

      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:

      @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')
        } 
      }
      

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            emilles Eric Milles
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment