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

Dynamically created closure handles delegate differently from statically created closure

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Closed
    • Major
    • Resolution: Information Provided
    • 1.5.4
    • None
    • groovy-runtime
    • None
    • CentOS 5 n Intel with java 1.6.0_03-b05

    Description

      The following script executes two closures. One is statically defined in class Test,
      the other is returned by Test.makeClosure(). In each case the delegate of the
      Closure is set to be an instance of class Delegate, but in one case a call to
      method "foo()" appears to resolve to Test.foo(), while in the second it appears
      to resolve to Delegate.foo().

      I showed this to Dierk at the G2 conference in Reston, and he asked me to post
      it as a bug.
      =====================================================================

      class Test {

      def foo()

      { "NOT the delegate" }

      def makeClosure() {
      def closure =

      { println foo() println delegate.foo() }

      }

      static staticClosure =

      { println foo() println delegate.foo() }

      }

      class Delegate {
      def foo()

      { "delegate" }

      }

      Closure dynamicClosure = new Test().makeClosure()
      dynamicClosure.setDelegate(new Delegate())
      dynamicClosure.call()

      Test.staticClosure.setDelegate(new Delegate())
      Test.staticClosure.call()
      ==================================================================

      Expected Output:

      delegate
      delegate
      delegate
      delegate

      Actual Output:

      NOT the delegate
      delegate
      delegate
      delegate

      Attachments

        Activity

          People

            Unassigned Unassigned
            bacota Bruce Cota
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: