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

Closure inside trait cannot use Closure's delegate variables

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4.15, 3.0.0-alpha-3, 2.5.2
    • Fix Version/s: 4.0.0-alpha-1, 3.0.6
    • Component/s: None
    • Labels:
      None
    • Environment:
      Groovy Version: 3.0.0-alpha-3 JVM: 1.8.0_171 Vendor: Oracle Corporation OS: Mac OS X

      Description

      The following code throws an error at runtime:

       

      trait T {
          def doIt() {
              return {
                  n = 1
              }
          }
      }
      
      class Delegate {
          int n
      }
          
      class Tish implements T {
          def go() {
              def closure = doIt()
              def d = new Delegate()
              closure.delegate = d
              closure()
              assert d.n == 1
              println "All good!"
          }
      }
      
      new Tish().go()

       

      This is the error I see on Groovy 3.0.0-alpha-3 (same as previous versions):

       

      groovy.lang.MissingPropertyException: No such property: n for class: Tish

       

      This is expected to work because, outside of traits, it does: this runs successfully:

       

      class T {
          def doIt() {
              return {
                  n = 1
              }
          }
      }
      
      class Delegate {
          int n
      }
          
      class Tisha extends T {
          def go() {
              def closure = doIt()
              def d = new Delegate()
              closure.delegate = d
              closure()
              assert d.n == 1
              println "All good!"
          }
      }
      
      new Tisha().go()

       

        Attachments

          Activity

            People

            • Assignee:
              emilles Eric Milles
              Reporter:
              renatoathaydes Renato Athaydes
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: