eprintln
func eprintln[T](value: T): voideprintln writes String.from(value) and a newline to standard error, for a warning or a usage line. eprintln() with no argument writes only the newline.
Details
- Standard error is not buffered, so
eprintlnand println come out in the order the program calls them. eprintlnignores a failed write, except a write to a stream whose reader has gone (a broken pipe), which ends the program with status 141. To learn of a failed write, call File.writeLine on Os.stderr, which returns aResult.
Basic examples
Write a warning to standard error. Standard output holds only what println writes:
println("total: 42")
eprintln("warning: 3 rows skipped")total: 42Print a usage line and exit with a nonzero status when an argument is missing:
let args = Os.args()
if len(args) != 1 {
eprintln("usage: greet <name>")
Os.exit(2)
}
println("Hello, ${args[0]}")Scope
Any value works. A string is written as it is; other values are written as String.from renders them:
type Point = { x: int, y: int }
eprintln("plain text")
eprintln(42)
eprintln(3.0)
eprintln(null)
eprintln([1, 2, 3])
eprintln(Point{ x: 1, y: 2 })
eprintln(Map{ "a": 1 })Standard error then holds:
plain text
42
3.0
null
[1, 2, 3]
Point{ x: 1, y: 2 }
Map{ "a": 1 }With no argument, eprintln writes an empty line:
eprintln("first")
eprintln()
eprintln("after a blank line")Interpolate to build a message from several values:
let path = "data.csv"
let line = 17
eprintln("${path}:${line}: missing column")At the end of a pipe, the piped value becomes the argument:
let skipped = 3
"skipped ${skipped} files" | eprintln()Applications
Report an error from a Result and keep going:
let paths = ["missing.txt"]
for path in paths {
let r = File.read(path)
if r is Err {
eprintln("error: ${r.error.message}")
continue
}
println(r.value)
}
println("done")doneKeep the data on standard output clean, so ant run report.ant > report.csv writes only the rows and the warning still reaches the terminal:
let rows = [["Ann", "31"], ["Bob", ""]]
for row in rows {
if row[1] == "" {
eprintln("warning: no age for ${row[0]}")
continue
}
println(String.join(row, ","))
}Ann,31End a line of progress dots that eprint wrote:
for _ in Iter.range(0, 5) {
eprint(".")
}
eprintln()
eprintln("5 steps done")Properties and relations
eprintln(x) writes the same text as eprint("${x}\n"), since interpolation renders with String.from too:
let x = [1, 2]
assert("${x}" == String.from(x))
assert(String.from("text") == "text")
assert(String.from(["a", "b"]) == "[\"a\", \"b\"]")Possible issues
eprintln takes one value. Interpolate to write several:
let name = "Ann"
let age = 31
eprintln(name, age) // error: eprintln takes 0 to 1 arguments, got 2eprintln is generic, so as a function value it needs a function type that fixes its type parameter:
let warn = eprintln
// error: eprintln is generic; call it, or give the expected function type that fixes its type parameterslet warn: func(string): void = eprintln
warn("disk almost full")The call with no argument exists only as a call. The function value takes one argument:
let blank: func(): void = eprintln // error: cannot use eprintln where func(): void is expectedeprintln is not a function on a type, so it cannot be called through .:
"oops".eprintln() // error: string has no field or function eprintlnSee also
eprint, println, print, Os.stderr, File.writeLine, Os.exit, String.from
