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

Class Implementing Map Can't Read Fields From Within Closures

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Bug
    • 3.0.9
    • None
    • None
    • None

    Description

      If a class implements 'Map' or subclasses an implementation of 'Map' then closures can't read fields in that class. This code is in a script called 'map.groovy':

       

      class A extends HashMap {
        @Override
        Object get(Object key) {
          return 'OtherString'
        }
      }
      class B extends A {
        String aa = 'TheString'    
        def foo() {
          println aa
          Closure c = {println aa}
          c.call()
        }
      }
      new B().foo()
      $ groovy map.groovy 
      TheString
      OtherString

      I expect to get "TheString" twice.

       

      I see the same behavior with other implementations of 'Map' or when I implement 'Map' directly.

      Curiously I was not able to get the same behavior when I created my own interface defining 'get()' and implementing that.

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            tofor Ian
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: