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

println in closure doesn't respect "out" PrintWriter binding

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.0-JSR-4
    • 1.5.1
    • groovy-jdk
    • None
    • Windows, groovy-all-1.0-jsr-4

    Description

      Using GroovyShell, I accidently discovered the very cool feature that binding "out" to a PrintWriter means that:


      print "foo"


      Goes to Script.println, finds "out", and "foo" ends up there instead of System.out. For working in a servlet environment, this is indeed awesome.

      However, with closures:


      ["a", "b"].each

      { println it }

      The println used is DefaultGroovyMethods.println, which does not do the same binding/property lookup for "out" and hence the output is "swallowed" into System.out.println, at least compared to the non-closure println.

      So, DefaultGroovyMethods.println (IMHO) should do the same "out" binding lookup for a user-provided writer.

      If "out" is too generic of a name to use for all of Groovy (instead of just people who use Script.println), a more verbose binding name like "groovy.lang.out" would be cool, but whatever, it just threw me for a loop that the in-closure println works differently from the the script-println.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            stephenh Stephen Haberman
            Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: