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

@CompileStatic on class using with(Closure) leads to compilation error.

    XMLWordPrintableJSON

Details

    Description

      import groovy.transform.CompileStatic
      
      interface Bar {
        String getField()
      }
      
      class BarImpl implements Bar {
        String field
      }
      
      class Foo {
        Bar bar
      
        Foo(String field) {
          this.bar = new BarImpl(field: field)
        }
      }
      
      @CompileStatic
      class Tester {
        void test() {
          Foo foo = new Foo('value')
          println foo.bar.with { "$field" }
        }
      }
      
      new Tester().test()
      

      when compiled with groovyc results in the following compilation error:

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      bad.groovy: 23: [Static type checking] - The variable [field] is undeclared.
       @ line 23, column 28.
         		println foo.bar.with { "$field" }
                                    ^
      
      1 error
      

      However, without the @CompileStatic on the Tester class, the code compiles cleanly and runs successfully.

      From what I've been able to discern, the compilation error does not occur if the with() occurs on the 'top-level' object and only occurs when the contained/subordinate object is an interface.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            acourtneybrown Adam Brown
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: