print
func print[T](value: T): voidprint writes String.from(value) to standard output, with no newline. Output is not buffered, so it comes out in program order. A failed write is ignored, except a broken pipe, which ends the program with status 141 as SIGPIPE does; File.writeLine(Os.stdout(), …) reports other errors.
Details
printwrites no newline. End the line with println, or put\nin the string.- It writes what String.from gives for the value, so a string is written without quotes, and a string inside an array or a record is quoted.
- Standard output is not buffered, so text written with
printappears at once, before a read that follows it. A program that writes much output writes it through a File.buffered handle on Os.stdout instead. - A broken pipe ends the program with status 141, so
ant run gen.ant | headstops onceheadhas read enough.
Basic examples
Write two pieces of text on one line, then end it:
print("Hello")
print(", world")
println()Hello, worldPrint a prompt and read the answer on the same line:
print("name: ")
let name = try Os.stdin().readLine()
println("Hello, ${name ?? "stranger"}")name: Hello, AnnScope
Any value is written as String.from renders it. A string at the top level has no quotes; inside an array it has them:
type Point = { x: int, y: int }
type UserId = string
print(42)
print(" ")
print(1.0)
print(" ")
print(null)
print(" ")
print(Point{ x: 1, y: 2 })
print(" ")
print(UserId("u-1"))
print(" ")
print(["a", "b"])
print(" ")
print("é")42 1.0 null Point{ x: 1, y: 2 } UserId("u-1") ["a", "b"] éAn empty string writes nothing, and a \n inside the string ends a line:
print("")
print("one\ntwo\n")one
twoprint takes the value through a pipe or by name, like any function:
"piped " | print()
print(value: "named\n")piped namedprint is generic, so as a function value it needs a concrete type where a function is expected:
let show: func(int): void = print
for n in [1, 2, 3] {
show(n)
}
println()123Applications
Join items with a separator as they come, writing the separator before every item but the first:
let words = ["one", "two", "three"]
for entry in words.entries() {
if entry.index > 0 {
print(", ")
}
print(entry.value)
}
println()one, two, threePrint a table one cell at a time, ending each row with println:
let rows = [[1, 20, 300], [4000, 5, 60]]
for row in rows {
for n in row {
print(String.from(n).padStart(6))
}
println()
} 1 20 300
4000 5 60Show a dot per finished step on one line:
for _ in ["fetch", "build", "test"] {
print(".")
}
println(" done")... doneProperties and relations
print(v) followed by println() writes the same line as println(v):
let value = Map{ "a": 1 }
print(value)
println()
println(value)Map{ "a": 1 }
Map{ "a": 1 }print(v) writes the same text as File.writeText on Os.stdout, except that a failed write returns no Result:
let value = [1, 2]
print(value)
try File.writeText(Os.stdout(), String.from(value))[1, 2][1, 2]Possible issues
print needs an argument; only println may be called with none:
print() // error: print needs an argument for valueprint takes one value. Interpolate several into one string instead:
let name = "Ann"
let age = 31
print(name, age) // error: print takes 1 argument, got 2+ does not join a string and a number. Use interpolation:
let count = 3
print("count: " + count) // error: + needs both operands of one type, got string and int
print("count: ${count}\n")Without a type to fix its type parameter, print is not a value:
let show = print
// error: print is generic; call it, or give the expected function type that fixes its type parametersA print made while a File.buffered handle on Os.stdout holds output goes past the buffer and can come out first:
let out = Os.stdout().buffered()
try out.writeText("A")
print("B")
try out.flush()
println()BAWrite everything through the buffered handle, or flush it before calling print.
See also
println, eprint, eprintln, String.from, Os.stdout, File.writeText, File.buffered
