Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-2503 MOP 2.0 design inflluencing issues
  3. GROOVY-3870

When adding methods to Closure through EMC, the delegate of the closure doesn't point at the closure itself

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 1.7-beta-2
    • None
    • groovy-runtime
    • None
    • Patch

    Description

      When adding methods to classes through EMC, with the SomeClass.metaClass.someMethod notation, the delegate of the closure we assign to someMethod represents the instance of SomeClass. However, when adding methods on Closures through EMC, the delegate variable in the closure is not pointing at the closure instance itself, but the script or the surrounding class.

      Example #1 in a class:

      class Foo {
          def bar() {
              Closure.metaClass.baz = { -> assert delegate instanceof Closure }
              def c = {}
              c.baz()
          }
      }
      new Foo().bar()
      

      In that case, the delegate is the new Foo() instance, instead of being the closure we assign to baz.

      Example #2 in a script:

      Closure.metaClass.baz = { -> assert delegate instanceof Closure }
      def c = {}
      c.baz()​
      

      In that case, the delegate is the current script.

      Attachments

        Activity

          People

            Unassigned Unassigned
            guillaume Guillaume Sauthier
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated: