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

Closures can't modify variables more than 1 lexical level up

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-beta-9
    • 1.0-beta-10
    • None
    • None
    • windows xp + cygwin, sun jdk 1.5.0_01-b08

    Description

      This small test script illustrates the problem:

      1. begin script
        def ctest()
        Unknown macro: { a = -1 cl1 = { a = 2 }
        cl1()
        println "a is now: ${a}"

        a = -1
        cl2 = {
        cl3 = { a = 2 } cl3() }

        cl2()
        println "a is now: ${a}"
        }

      ctest()

      1. end script

      Running the code above produces:

      a is now: 2
      a is now: -1

      It should have changed a to 2 both times. It may be non-obvious
      from this trivial test script, but the situation occurs frequently
      enough to be a significant obstacle to development. Workaround is
      to refactor code so that it never has more than one nesting level.

      Attachments

        Activity

          People

            Unassigned Unassigned
            stevey Steve Yegge
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: