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

Closure shared variable type is polluted with unreached assignment type

    XMLWordPrintableJSON

    Details

      Description

      @groovy.transform.CompileStatic
      def cs() {
          def var
          
          var = "hi"
          println var.toUpperCase() // [Static type checking] - A closure shared variable [var] has been assigned with various types and the method [toUpperCase()] does not exist in the lowest upper bound of those types: [java.lang.Object]. In general, this is a bad practice (variable reuse) because the compiler cannot determine safely what is the type of the variable at the moment of the call in a multithreaded context.
          
          def c = {
              var = new Object()
          }
          c()
          var.toString()
      }
      
      assert cs() != null
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              daniilo Daniil Ovchinnikov
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: