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

Delegation inconsistent between methods and properties in nested closures

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.2, 2.3.7, 2.4.0-rc-1
    • 4.0.0-alpha-1
    • groovy-runtime

    Description

      When a closure's resolveStrategy is set to DELEGATE_ONLY or DELEGATE_FIRST, resolution is different in nested closures between methods and properties of the delegate. For example, in the following, x resolves to f's delegate (what I expect), but keySet() resolves to g's delegate.

      def g = {->
          def f = {
              {-> [x, keySet()]}()
          }
      
          f.resolveStrategy = Closure.DELEGATE_ONLY
          f.delegate = [x: 1, f: 0]
      
          f()
      }      
      
      g.delegate = [x: 0, g: 0]
      g()
      

      Result:

      [1, ['x', 'g']]

      Whereas without the nested closure

      def g = {->
          def f = {
              [x, keySet()]
          }
      
          f.resolveStrategy = Closure.DELEGATE_ONLY
          f.delegate = [x: 1, f: 0]
      
          f()
      }      
      
      g.delegate = [x: 0, g: 0]
      g()
      

      Result: [1, ['x', 'f']]

      Attachments

        1. DelegateTest.groovy
          0.6 kB
          Matthew Farrell

        Issue Links

          Activity

            People

              emilles Eric Milles
              farrellm Matthew Farrell
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m