groovyplayground icon indicating copy to clipboard operation
groovyplayground copied to clipboard

println only works in root context of the script

Open timroes opened this issue 9 years ago • 1 comments

Using a println statement inside a class will log to the server and not capture the output and send it to the client.

timroes avatar Mar 15 '15 10:03 timroes

I found a work-around for this. I use Closures, with the following sample

// Define a closure.
def printLine = {println(it)}

class example {
    private def _cl = null
    public example(cl) {
        _cl = cl
    }
    public def printTest(String value) { 
        _cl(value)
    }
}

//this should print "test" as expected.
def test = new example(printLine);
test.printTest("test")

In case anyone else is using this service regularly and need to conveniently print lines in a class...

krum110487 avatar Sep 14 '20 02:09 krum110487