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

Captured types doesn't work in @CompileStatic

    Details

      Description

      This code could be successfully compiled and executed.

      @CompileStatic
      static def method(List<? extends Serializable> captured) {
          captured.add('some string')
          return captured
      }
      
      println method(new ArrayList<Integer>())
      

      Looks like type check in groovy doesn't work with captured types. Is it a bug?

      Is there any documentation about generics in groovy?

      P.S.:
      Another kind of strange behaviour:

      @CompileStatic
      static def method() {
          List<Integer> list = new ArrayList<>()
          List<? extends Serializable> captured = list
          captured.add('some string') //Error:(7, 5) Groovyc: [Static type checking] - Cannot call java.util.ArrayList <java.lang.Integer>#add(java.lang.Integer) with arguments [java.lang.String]
          return captured
      }
      

      Somehow type checking infered type of 'captured' variable to java.util.ArrayList <java.lang.Integer>

        Attachments

          Activity

            People

            • Assignee:
              daniel_sun Daniel Sun
              Reporter:
              Alexey Afanasiev Alexey Afanasiev
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: