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

regression of return type from ternary operator

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

Details

    Description

      In Groovy 2.4.5 the following code works, but in Groovy 2.4.6, it fails to compile:

      @groovy.transform.Field
      String x = "X"
      
      @groovy.transform.CompileStatic
      public List<String> getStrings() {
      	x ? [x] : Collections.emptyList()
      }
      
      getStrings()
      

      In Groovy 2.4.6 the compile error is:

      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.List <? extends java.lang.Object> to: java.util.List <String>
       @ line 6, column 2.
          x ? [x] : Collections.emptyList()
          ^
      

      Workaround is to cast the result of emptyList to the erased type, (List) in this case.

      Attachments

        Issue Links

        Activity

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

          People

            daniel_sun Daniel Sun
            gillius Jason Winnebeck
            Votes:
            2 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment