groovy
  1. groovy
  2. GROOVY-4886

Nested with statements do not access global class variables

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Not A Bug
    • Affects Version/s: 1.7.8
    • Fix Version/s: None
    • Component/s: parser
    • Labels:
    • Number of attachments :
      0

      Description

      Nested with statements do not seem to recognize global (or super) parameters:

      class A {
      def foo = 123
      def controller = [params: [bar: 1]]
      def test() {
      controller.with {
      params.with

      { bar = foo }

      }
      controller.params.bar
      }
      }

      def a = new A()
      assertEquals 123, a.test()//Fails

        Activity

        Hide
        blackdrag blackdrag added a comment -

        If you have an expression using an implicit this in a closure, then the closure has first to find out what this actually means. Your line bar=foo is actually <this>.bar=<this>.foo, with <this> being the implicit this. To resolve this "with" tries first the delegate, which is the object you use "with" on. In this case here we have a map for this and a map will always return a value, regardless for what key you ask. It wil return null in that case. Since the map responds to everything the <this>.bar=<this>.foo will be controller.params.bar=controller.params.foo - leading to the wrong result for you.

        The solution for you is now to qualify and go away from the implicit this and use and explicit one: bar=this.foo

        So this is not a bug, it is how the scoping rules in Groovy work in combination with maps.

        Show
        blackdrag blackdrag added a comment - If you have an expression using an implicit this in a closure, then the closure has first to find out what this actually means. Your line bar=foo is actually <this>.bar=<this>.foo, with <this> being the implicit this. To resolve this "with" tries first the delegate, which is the object you use "with" on. In this case here we have a map for this and a map will always return a value, regardless for what key you ask. It wil return null in that case. Since the map responds to everything the <this>.bar=<this>.foo will be controller.params.bar=controller.params.foo - leading to the wrong result for you. The solution for you is now to qualify and go away from the implicit this and use and explicit one: bar=this.foo So this is not a bug, it is how the scoping rules in Groovy work in combination with maps.
        Hide
        ishults added a comment -

        This makes sense, thanks for the detailed response.

        Show
        ishults added a comment - This makes sense, thanks for the detailed response.

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            ishults
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: