eprint
func eprint[T](value: T): voideprint writes String.from(value) to standard error, with no newline. Neither stream is buffered, so the two interleave in program order.
Details
eprintwrites to standard error, not standard output, so a pipe or a>redirect does not capture it. Use it for progress, warnings, and prompts that are not part of a program's result.- It writes no newline. End the line with eprintln.
- It writes what String.from gives for the value, so a string is written without quotes.
- A failed write is ignored, except a broken pipe, which ends the program with status 141.
Basic examples
Start a line of status on standard error and finish it with eprintln once the work is done. Only the result reaches standard output, which the text block below shows:
eprint("loading config... ")
let port = 8080
eprintln("done")
println("port: ${port}")port: 8080Prompt on standard error, so that standard output holds only the answer:
eprint("name: ")
let name = try File.readLine(Os.stdin())
println("Hello, ${name ?? "stranger"}")Hello, AnnScope
Any value is written as String.from renders it: numbers, records, arrays, null, and strings without quotes:
type Point = { x: int, y: int }
eprint(42)
eprint(" ")
eprint(Point{ x: 1, y: 2 })
eprint(" ")
eprint([1, 2, null])
eprint(" ")
eprint("é\n")Standard error receives 42 Point{ x: 1, y: 2 } [1, 2, null] é and a newline.
An empty string writes nothing:
eprint("")eprint is a function value too. Give it a concrete type where a function is expected:
let warn: func(string): void = eprint
warn("disk almost full\n")Applications
Show progress on standard error while standard output carries the data. A pipe receives only the names; a person at the terminal sees a dot per file as well:
let files = ["a.txt", "b.txt", "c.txt"]
for file in files {
eprint(".")
println(file.toUpper())
}
eprintln(" done")A.TXT
B.TXT
C.TXTReport a usage error and exit with a nonzero status:
let args = Os.args()
if len(args) == 0 {
eprint("usage: ")
eprintln("${Path.base(Os.program())} <file>...")
Os.exit(2)
}
println(args[0])Properties and relations
eprint(v) writes the same text as File.writeText on Os.stderr, except that a failed write returns no Result:
let value = [1, 2]
// Each of the next two lines writes [1, 2] to standard error.
eprint(value)
try File.writeText(Os.stderr(), String.from(value))Possible issues
eprint needs an argument. Only println and eprintln may be called with none:
eprint() // error: eprint needs an argument for valueIn a test, ant test collects what eprint writes and shows it only when the test fails, or with -v. A passing test prints nothing.
A result written with eprint is lost to a pipe or a > redirect. Write what a program produces with print or println, and keep eprint for messages about it.
See also
eprintln, print, println, Os.stderr, File.writeText, String.from
