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

@Field variable access within closures broken

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.15, 3.0.3
    • 4.0.0-alpha-1, 3.0.5, 2.5.13
    • Compiler
    • None

    Description

       

      @groovy.transform.Field String abc
      binding.variables.clear()
      abc = "abc"
      println binding.hasVariable("abc")
      ["D","E","F"].each { abc += it }
      println binding.getVariable("abc")
      assert abc == "abcDEF"
      

      If a variable is declared using the @Field annotation, it can be assigned to in the main script body, but any assignment inside a closure (here the closure used with each) ends up in the script binding instead. Assignments within regular methods work fine, too. If the variables is accessed inside the closure and it is not in the binding, then the value will come from the field. As soon as it is in the binding, the binding variable takes precedence over the field. In both cases the field declared with @Field needs to have precedence over anything that is in the binding, just like local variables do.

       

       

      Attachments

        Activity

          People

            emilles Eric Milles
            eddelplus Jochen Eddelbuettel
            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