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

closures do not handle 'it' correctly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Not A Problem
    • 1.8.5
    • None
    • groovy-jdk
    • Windows command line (to reduce dependencies)

    Description

      Running the following script shows 'it' as being null:

      dec = { it ->
      def x = it
      return { println "${x--} (was $it)" }
      }

      def f = dec(2)
      f(); f();

      Output is:
      2 (was null)
      1 (was null)

      If the 'it' references are changed to 'val', then it works fine - i.e. says '(was 2)'

      This also fails when no parameter is used (i.e. defaulting to 'it')

      Attachments

        Activity

          People

            Unassigned Unassigned
            ssstratton Andrew Stratton
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: