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

STC: explicit declared variable type ignored in favor of assigned value type(s)

    XMLWordPrintableJSON

Details

    Description

      Follow up to GROOVY-9058.  Consider the following:

      List getSomeRows() { ... }
      @groovy.transform.CompileStatic
      void meth() {
        List<Object[]> rows = getSomeRows()
        rows.each { row ->
          def col = row[0]
        }
      }
      

      The inferred type of rows is List and not List<Object[]> even though the assignment cleared type checking. This causes the inferred type of row to be Object instead of Object[].

      Similarly, List<String> list = [] infers as ArrayList<String> instead of the explicit declared type, and Map<String, ?> map = [:] infers to LinkedHasMap<...> instead of the declared type. In general, I think as long as the assignment is compatible, the variable should retain its explicitly declared type regardless of assignments.

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              emilles Eric Milles
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: