println
func println[T](value: T): voidprintln writes String.from(value) and a newline to standard output. println() with no argument writes only the newline.
Details
- A string is written without quotes; a string inside an array, a map, or a record keeps its quotes.
- Standard output is not buffered, so each call writes at once, in program order with print and eprintln. A program that writes many lines can buffer them with File.buffered.
- A failed write is ignored, except a broken pipe, which ends the program with status 141. Write with File.writeLine to get a
Resultinstead.
Basic examples
Write a line:
println("Hello, Ant")Hello, AntInterpolate values into the line:
let name = "Ann"
let age = 31
println("${name} is ${age}")Ann is 31Scope
Numbers, booleans, and null:
println(42)
println(3.0)
println(1.0 / 3.0)
println(true)
println(null)42
3.0
0.3333333333333333
true
nullA string is written as it is. Inside a collection, strings are quoted:
println("say \"hi\"")
println(["say", "hi"])say "hi"
["say", "hi"]Records, union members, and named primitive types are written with their type name:
type Point = { x: int, y: int }
type User = { name: string, age: int }
type Circle = { radius: float64 }
type Square = { side: float64 }
type Shape = Circle | Square
type Count = int
let shape: Shape = Circle{ radius: 1.5 }
println(Point{ x: 1, y: 2 })
println(User{ name: "Ann", age: 31 })
println(shape)
println(Count(3))Point{ x: 1, y: 2 }
User{ name: "Ann", age: 31 }
Circle{ radius: 1.5 }
Count(3)Arrays, maps, sets, Results, and Bytes:
let parsed: Result[int, Error] = Ok{ value: 7 }
println([1, 2, null])
println(Map{ "a": 1, "b": 2 })
println(Set.of([1, 2]))
println(parsed)
println("hi".toBytes())[1, 2, null]
Map{ "a": 1, "b": 2 }
Set.of([1, 2])
Ok{ value: 7 }
Bytes(68 69)An Html value is written as its markup. Interpolated text is escaped:
let name = "Ann & Bob"
println(<p>Hello, {name}</p>)<p>Hello, Ann & Bob</p>With no argument, println writes an empty line. An empty string does the same:
println("first")
println()
println("")
println("last")first
lastA newline inside the string starts a new line of output:
println("one\ntwo")one
twoprintln is a function value too. Give it a concrete type where a function is expected:
let show: func(int): void = println
for n in [1, 2, 3] {
show(n * n)
}1
4
9Applications
Print a small report, one line per entry:
let stock = Map{ "apples": 12, "pears": 0, "plums": 7 }
for { key, value } in stock {
let note = if value == 0 { " (sold out)" } else { "" }
println("${key.padEnd(8, " ")}${value}${note}")
}apples 12
pears 0 (sold out)
plums 7Print a table with a separator line:
let header = ["name", "age"]
let rows = [["Ann", "31"], ["Bob", "27"]]
println(String.join(header, " | "))
println("-".repeat(10))
for row in rows {
println(String.join(row, " | "))
}name | age
----------
Ann | 31
Bob | 27Write many lines through a buffer, and flush it at the end:
let out = Os.stdout().buffered()
for n in Array.range(1, 4) {
try out.writeLine("line ${n}")
}
try out.flush()line 1
line 2
line 3Properties and relations
println(v) writes the same text as print of v followed by "\n":
let value = [1, 2]
println(value)
print(value)
print("\n")[1, 2]
[1, 2]The text println writes before the newline is the text String.from returns:
type Point = { x: int, y: int }
assert(String.from(Point{ x: 1, y: 2 }) == "Point{ x: 1, y: 2 }")
assert(String.from("hi") == "hi")
assert(String.from(null) == "null")println(v) writes what File.writeLine writes on Os.stdout, except that a failed write returns no Result:
let value = 42
println(value)
try File.writeLine(Os.stdout(), String.from(value))42
42Possible issues
println takes at most one argument. Join the parts with interpolation instead:
let a = 1
let b = 2
println(a, b) // error: println takes 0 to 1 arguments, got 2let a = 1
let b = 2
println("${a} ${b}")1 2As a function value, println is generic, so it needs a type where it is used:
let show = println
// error: println is generic; call it, or give the expected function type that fixes its type parametersThe no-argument form exists only in a call. A func(): void cannot hold println:
let blank: func(): void = println // error: cannot use println where func(): void is expectedlet blank: func(): void = { println() }
blank()println writes to standard output, so a pipe or a > redirect captures it. Write warnings and progress with eprintln so they stay out of the captured output.
See also
print, eprintln, eprint, String.from, File.writeLine, File.buffered, Os.stdout, string interpolation
