Groovy
  1. Groovy
  2. GROOVY-4886

Nested with statements do not access global class variables

    Details

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

      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

        ishults created issue -
        Hide
        Jochen Theodorou 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
        Jochen Theodorou 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.
        Jochen Theodorou made changes -
        Field Original Value New Value
        Status Open [ 1 ] Closed [ 6 ]
        Resolution Not A Bug [ 6 ]
        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.
        Mark Thomas made changes -
        Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
        Mark Thomas made changes -
        Workflow jira [ 12733813 ] Default workflow, editable Closed status [ 12745632 ]
        Mark Thomas made changes -
        Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
        Mark Thomas made changes -
        Workflow jira [ 12971357 ] Default workflow, editable Closed status [ 12979145 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Closed Closed
        14m 18s 1 Jochen Theodorou 16/Jun/11 14:14

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development