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

modify variables outside closures, then recalling the closure does not use new value

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.0-beta-3
    • None
    • None

    Description

      a = 0
      b = 1

      x =

      { System.out.println('on entry to closure a = ' + a) System.out.println('on entry to closure b = ' + b) a++ System.out.println('on exit from closure a = ' + a) System.out.println('on exit from closure b = ' + b) }

      x.setDelegate(this)

      System.out.println('before first call a = ' + a)
      System.out.println('before first call b = ' + b)

      x.call()

      System.out.println('after first call a = ' + a)
      System.out.println('after first call b = ' + b)
      System.out.println()

      a *= 2
      b *= 2

      System.out.println('before second call a = ' + a)
      System.out.println('before second call b = ' + b)

      x.call()

      System.out.println('after second call a = ' + a)
      System.out.println('after second call b = ' + b)

      This produces:

      before first call b = 1
      on entry to closure a = 0
      on entry to closure b = 1
      on exit from closure a = 1
      on exit from closure b = 1
      after first call a = 1
      after first call b = 1

      before second call a = 2
      before second call b = 2
      on entry to closure a = 2
      on entry to closure b = 1
      on exit from closure a = 3
      on exit from closure b = 1
      after second call a = 3
      after second call b = 2

      You will see that b is still 1 in the second call to the closure.

      Attachments

        Activity

          People

            Unassigned Unassigned
            jstrachan James Strachan
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: