Explorer
Explorer

Reputation: 1647

Scala process logger

I am following the example for Scala Process Logger by Alvin Alexander which talks about how to handle stdout and stderr from external system commands. I get most part of the example and now understands how to use it but I am finding hard to understand the purpose of append _. Can someone please help me in understanding it? The documents is pretty vague for it.

#!/bin/sh
exec scala "$0" "$@"
!#
import sys.process._

val stdout = new StringBuilder
val stderr = new StringBuilder
val status = "ls -al FRED" ! ProcessLogger(stdout append _, stderr append _)

println(status)
println("stdout: " + stdout)
println("stderr: " + stderr)

Upvotes: 0

Views: 2082

Answers (2)

Charlie 木匠
Charlie 木匠

Reputation: 2390

stdout append _

Is equal to:

{ _ => stdout.append(_) }

Upvotes: -1

jwvh
jwvh

Reputation: 51271

Here's how ScalaDoc describes one of the ProcessLogger constructors:

def apply(fout: (String) ⇒ Unit, ferr: (String) ⇒ Unit): ProcessLogger 

So it takes two arguments, each taking a String input with no output (i.e. Unit). The argument stdout append _ is appending a String input (that's the _) to a StringBuilder.

In other words, stdout append _ can be rewritten as str => stdout.append(str)

Upvotes: 4

Related Questions