Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Minor
-
Resolution: Fixed
-
Affects Version/s: 1.0-JSR-4
-
Fix Version/s: 1.5.1
-
Component/s: groovy-jdk
-
Labels:None
-
Environment:Windows, groovy-all-1.0-jsr-4
-
Number of attachments :
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.
The problem is that println is a method on Object and so the println method from Closure is called then. Normally a method call from inside the closure should not affect the closure. but as long as we can't know what is inside and outside we can't resolve this. This is a job for the new MOP, so John gets the issue