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

setDelegate doesn't work when 'def' is used

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-JSR-3
    • 1.0-JSR-4
    • groovy-jdk
    • None

    Description

      //demo.groovy
      def c = {
      def x;
      println x;
      }
      c.setDelegate(x:'hello');
      c();

      The output is 'null'.

      //demo2.groovy
      def c = {
      //def x;
      println x;
      }
      c.setDelegate(x:'hello');
      c();

      the output is 'hello'.

      Moreover,
      class Test {
      static main(args) {
      def c = {
      def x;
      println "x:${x}"
      }
      c.setDelegate(x:'hello')
      c()
      }
      }

      The output is 'x:null'

      We can't remove 'def x' from Test class, otherwise, there is syntax error in this class(The variable x is undefined in the current scope).

      Attachments

        Activity

          People

            tug John Wilson
            wangbin wangbin
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: