IO
import "std/io" (included in import "std") · namespace std.io
Console input and output beyond the built-in print and input: printing without a line break, printing to standard error, and reading typed values. The most used helpers are also available as plain functions: printRaw, eprint, readLine, prompt, readInt.
grav
import "std"
fn main() {
printRaw("no line break, ")
IO.write(42)
print("")
IO.eprint("this goes to standard error")
let name = readLine() // "" at the end of input
print("[${name}]")
}no line break, 42
[]| Function | Does |
|---|---|
IO.print(x), print(x) | writes a value and a line break |
IO.write(x), printRaw(s) | writes without a line break |
IO.eprint(x), eprint(s) | writes a line to standard error |
IO.flush() | sends buffered output now |
IO.readLine(), readLine(), input() | one line, without its line break; "" at the end |
IO.prompt(msg), prompt(msg) | writes msg, then reads a line |
IO.readInt(), readFloat(), readBool() ... | reads a line and converts it; ValueError when it is not a number (Format) |
IO.promptInt(msg), IO.promptFloat(msg) | writes msg, then reads the answer as a number |
IO.readAll(), IO.readLines() | everything left on standard input |
Standard output is buffered when it is not a terminal; it is flushed before anything is written to standard error and when the program ends.
API
class IO
Static members
static fn print(message: string)— Printing: a string, or any other value (formatted like print()).static fn print(value: any)— Prints a value and a newline (same as print).static fn write(message: string)— no newlinestatic fn write(value: any)— Prints a value without a newline.static fn eprint(message: string)— Prints a message and a newline to standard error.static fn eprint(value: any)— Prints a value and a newline to standard error.static fn flush()— Writes out anything buffered for standard output.static fn input() -> string— Reading (each read takes one line; "" at end of input).static fn readLine() -> string— One line of standard input ("" at the end).static fn prompt(message: string) -> string— Writesmessage(no newline) and reads the answer line.static fn readInt() -> int— Reads a line as an int (ValueError when it is not one; see Format.toInt).static fn readLong() -> long— Reads a line as a long.static fn readFloat() -> float— Reads a line as a float (ValueError when it is not a number).static fn readDouble() -> double— Reads a line as a double.static fn readBool() -> bool— Reads a line as a bool ("true", "1", "yes", "y", "on").static fn promptInt(message: string) -> int— Writesmessageand reads the answer as an int.static fn promptFloat(message: string) -> float— Writesmessageand reads the answer as a float.static fn readAll() -> string— Everything left on standard input.static fn lines() -> FileReader— The remaining input, one line at a time as the loop asks (for input too big to hold):for (line in IO.lines()) { ... }.static fn readLines() -> Array<string>— The remaining input, line by line.
Functions
fn readLine() -> string— One line of standard input ("" at the end).fn prompt(message: string) -> string— Writesmessage(no newline) and reads the answer line.fn readInt() -> int— Reads a line as an int (ValueError when it is not one).fn readFloat() -> float— Reads a line as a float (ValueError when it is not a number).fn readBool() -> bool— Reads a line as a bool.fn promptInt(message: string) -> int— Writesmessageand reads the answer as an int.fn eprint(message: string)— Print to standard error (diagnostics) instead of stdout.fn printRaw(message: string)— Print without a trailing newline.