Yes, I saw it. Expanding where it left out:
Bill> I discovered just now that Scala's println seems to be caching
the
Bill> System.out reference somehow. Is there a reason for that?
Paul> That's how it was designed. Predef.println is Console.println.
You can
Paul> set Console.out to whatever you want.
# In the REPL: Console.setOut does not do what I expect as it's per
thread (some newlines removed)
scala> val newOut = new java.io.PrintStream(new
java.io.FilterOutputStream(System.out) { override def write(i:Int) { 1
to 2 foreach(_ => out.write(i)) } })
newOut: java.io.PrintStream = java.io.PrintStream@1b3f57d1
scala> Console.setOut(newOut)
scala> println("foo")
foo
scala> System.setOut(newOut)
scala> System.out.println("bar")
bbaarr
scala> println(Thread.currentThread); Console.setOut(newOut)
Thread[Thread-7,5,main]
scala> println(Thread.currentThread); Console.setOut(newOut);
println("foo")
Thread[Thread-8,5,main]
ffoooo
# Exhibit 2: On the other hand, this works fine, as new thread inherit
the Console.out from their parent.
Console.setOut(newOut)
import java.util.concurrent._
val executor = Executors.newFixedThreadPool(2)
def runnable = new Runnable { def run() { println("run") } }
executor.submit(runnable)
executor.submit(runnable)
executor.shutdown
My feeling is that the current Console.setOut is complex to use as one
has to worry about threading.
Interestingly, this gives a sense what people use System.setOut for:
http://google.com/codesearch#search/&q=System.setOut&type=cs
In many case the Console.withOut method would work fine (usually for
testing).
--Jean-Laurent
On Jun 29, 10:39 pm, Jason Zaugg <
jza...@gmail.com> wrote: