Skip to reference

std.io

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.io — standard output, standard error, and standard input as functions: the facade over the language's print family and the s38 line-read builtin. Stability tier: core.

**Why a facade at all, when print is ambient.** print is a prelude intrinsic and stays the spelling ordinary code writes — this module does not replace it and could not (§1: one concept, one name). What it adds is the four operations as VALUES of the library rather than magic: a stream chosen by the function you call rather than by a name's prefix, the raw-versus-line difference spelled in the name, and one home for the input side, whose row is the interesting part and whose ambient spelling std may not reuse (see below). A program that computes what to write, or writes to stderr from a helper, reads better through these.

The format spec is the CALLER's, and it passes through untouched. Every function here takes an already-rendered str, so io.write_line("{n:>8}") is the caller's f-string with the caller's spec (spec §7.4, the s38 lowering) and this module adds nothing to it — no second interpolation pass, no reformatting, nothing that could disagree with what the literal says. The operations that BUILD a rendered string out of data live in std.fmt (§11's split), and this module is where the result goes.

**is_terminal (s215) is the one function here that takes a handle**, so this module imports std.fs for the File it asks about; it is the terminal question a prompt depends on, which is why it lives with the prompt rather than with the files.

**Capability: io (I13).** The four writers and the line read reach the host's standard streams, are tagged io in the toolchain's sandbox table, and are categorically refused at comptime (D33): a comptime fn cannot read a line, and — because a comptime program's output would be neither reproducible nor visible — cannot write one either.

Writing cannot fail here, and that is the builtin's contract, not a simplification. print/eprint return nothing and raise nothing: a closed stdout is not reportable at this pin. When the builtin tier grows a fallible write, these signatures gain a row and the change is a breaking one, which is why it is recorded here rather than pre-empted with a row that would always miss (F-0046).

The input side is two functions and one row between them. input_line raises eof at the end of input — an outcome, not a failure — plus io for a host failure and utf8 for bytes that are not text. input_all reads to the end and joins the lines with "\n", so eof is not in ITS row at all: the end of input is what makes it return.

input_all was a reviewed contract for four sprints and it is worth recording why, because the reason was never "nobody wrote it". The loop must stop on ONE tag and re-raise the others, and no handler could tell tags apart: a bare identifier in a handler BINDS rather than matching, so it catches everything (F-0043), and the shape that should have fixed it — else |e| match e { eof => …, io => … } — matched its FIRST ARM for every tag on the lane that executed, with no diagnostic (F-0052). A loop written over that would have treated a read error as the end of input, which is a silent truncation of someone's data, and §14 forbids shipping an operation whose failure mode a module cannot distinguish. s70's match tier and s71's payload-pattern ruling closed both, and the function is four lines and one handler.

What is still a reviewed contract is the byte-exact read:

- read_bytes(max: int) -> List[byte] ! {io} — standard input as it is. input_all joins LINES, and the line read strips terminators, so "a\nb" and "a\nb\n" are indistinguishable through this module — which is fine for text processing and wrong for anything byte-exact. No builtin returns bytes from standard input (F-0046, unmoved), so std ships nothing rather than a read_bytes that could only lie about the last byte.

**Why the reader is input_line and not read_line.** read_line is the name this module wanted, and it is a name std cannot have: it is an AMBIENT PRELUDE name, and a module item that shares one is resolved differently by the two implementations. Measured at these pins, from a pub fn read_line whose body called read_line(): wolfc resolved the inner call to the BUILTIN (the delegation worked), and lupin resolved it to the module's own item — unbounded recursion, reported as unsupported — call depth exceeded 512 frames. One source, a facade in one machine and a stack overflow in the other, with no diagnostic on either side (F-0047). So the reader takes a name of its own, the writers keep theirs (nothing ambient is called write), and the filing asks for what would let the good name come back: retire the ambient host names — the prelude's own comment already promises to, "the moment the real std surface replaces them" — and rule the shadowing question explicitly for the names that stay.

Lanes (re-measured at the sc11 pin). Both compiler rungs execute everything here — the native rung lowers the io tier as of #40, so both of this module's tests run natively where both were unsupported in sc07. Standard input is still EMPTY on the checked lane (conform-run has no stdin injection at this pin, F-0046 unmoved), so the eof path is the one the rig witnesses and the hit path is proven upstream. The writers' stdout is compared by hash; stderr is not in the record protocol, so write_err and write_err_line are witnessed by their exit status alone, which is stated rather than hidden. lupin gained eprint/eprint_raw at 0.1.5, which is what moved tests/io/writers_and_streams.lu to a lupin run row: all four writers work there now and only the READS are refused (this machine has no read_line and no injectable stdin, by design and with a sentence in the refusal — unchanged at 0.1.8). The capability posture is the rig's to record once rather than each test's to restate.

Types and traits

No public declarations in this section.

Functions and methods

is_terminal

fn · Source line 128

pub fn is_terminal(f: fs.File) -> bool ! {io}

Whether f names a terminal — the question an interactive program asks before it prompts: a shell reads with a prompt only when its input is one.

Rows: io when f is closed or forged — "not a terminal" and "not a handle" are different answers, so the second is never false. Never traps. fs.stdin(), fs.stdout() and fs.stderr() ask the process's own streams; a file or a pipe end (std.process.pipe) asks that handle.

Capability io; refused at comptime (D33). Delegates to os_isatty (wolf-lang s215, [os.fs.isatty]).

A prose example, because a doc example imports exactly the documented module (§4) and cannot reach std.fs for a File: under the test rig, which wires standard input to the null device, io.is_terminal(fs.stdin()) is false — a device, not a terminal — and tests/io/is_terminal_rows.lu holds it with a pipe end and a forged handle.

write

fn · Source line 147

pub fn write(text: str)

Write text to standard output exactly as given — no newline, no reformatting.

Total: no rows, no traps (see the module header on the builtin's infallible-write contract). Capability io; refused at comptime (D33). Delegates to print_raw.

This is the function to build a line out of pieces with, and the one to use when the bytes must be exact: a progress indicator, a prompt, a value that must not gain a terminator it did not ask for.

io.write("no newline here")
io.write("")

write_line

fn · Source line 162

pub fn write_line(text: str)

Write text to standard output followed by a newline.

Total: no rows, no traps. Capability io; refused at comptime (D33). Delegates to print, whose terminator is exactly one "\n" on every platform — no "\r\n" translation anywhere, which is what keeps a stdout hash comparable across the tier-1 hosts.

io.write_line("one line")
io.write_line("{2 + 2}")

write_err

fn · Source line 181

pub fn write_err(text: str)

Write text to standard error exactly as given — no newline.

Total: no rows, no traps. Capability io; refused at comptime (D33). Delegates to eprint_raw.

Standard error is the stream for what a program says ABOUT its work rather than what its work produced: diagnostics, progress, warnings. Keeping them off stdout is what lets a caller pipe the output of a wolf program into another one — and it is why the rig can hash stdout at all.

io.write_err("partial diagnostic ")
io.write_err("continued\n")

write_err_line

fn · Source line 193

pub fn write_err_line(text: str)

Write text to standard error followed by a newline.

Total: no rows, no traps. Capability io; refused at comptime (D33). Delegates to eprint.

io.write_err_line("a diagnostic line")

input_line

fn · Source line 224

pub fn input_line() -> str ! {eof, io, utf8}

The next line of standard input, without its terminator.

Rows: eof when there is no more input — the END of the stream is an outcome, so a read loop is while over this function's misses rather than a length test; io for a host failure; utf8 when the bytes are not well-formed UTF-8 (nothing is returned in that case — this is not a lossy decode). Never traps.

The terminator is consumed and dropped, and a "\r\n" ending yields the line without either byte — so a file written on windows reads the same as one written on linux, and a caller cannot tell whether the last line of the input ended with a newline. That last point is a real limitation of the line-oriented shape, not a bug: a caller who needs the bytes as they are wants read_all, which is a contract at this pin (F-0046).

Capability io; refused at comptime (D33). Delegates to the prelude's read_line, whose name this function deliberately does not share — see the module header.

A doc example cannot demonstrate the hit path: an extracted example runs with no stdin, so the honest example is the miss, which is also the one every reader gets wrong first.

(io.input_line() else "no input") == "no input"

prompt

fn · Source line 248

pub fn prompt(text: str) -> str ! {eof, io, utf8}

Write text to standard output with no newline, then read one line of standard input.

Rows: input_line's exactly — eof when the input ends (the user pressed the terminal's end-of-input, or the pipe closed), io for a host failure, utf8 for bytes that are not text. Never traps.

The prompt is written with write rather than write_line, because a prompt belongs on the same line as the answer, and it is not flushed separately: standard output is line-buffered on a terminal, so a prompt with no newline may appear only when the line is read. That is the host's behaviour and this function does not paper over it; a program that needs a guaranteed flush before a blocking read wants the flush operation, which no builtin offers yet (F-0046).

Capability io; refused at comptime (D33).

(io.prompt("name? ") else "anonymous") == "anonymous"

input_all

fn · Source line 287

pub fn input_all() -> str ! {io, utf8}

Every remaining line of standard input, joined with "\n".

Rows: io for a host failure and utf8 for bytes that are not text — both RE-RAISED unchanged from the line read they came from. eof is not in the row: the end of input is what makes this function RETURN, which is the whole difference between reading everything and failing to read something. Never traps.

What it can and cannot tell you. The result is the input's lines with exactly one "\n" between them and NONE at the end, whatever the input's own last byte was: the line read strips the terminator and nothing gives it back, so "a\nb" and "a\nb\n" both arrive here as "a\nb" (F-0046 asks for the byte-exact read_all that would fix it). A caller who must know wants that builtin; a caller who wants lines is not affected, and std.str.lines on this result gives them back. Empty input is "", which is also what a single empty line looks like, for the same reason.

Why this function exists only now. It was a reviewed contract for four sprints because the loop it needs must stop on ONE tag and re-raise the others, and no handler could tell tags apart: a bare binding catches everything (F-0043) and a match inside a handler matched its first arm for every tag on the lane that executed (F-0052). Both are closed at this pin — the match tier at s70, the payload pattern at s71 — so the handler below discriminates on all three lanes, and the alternative std refused to ship is visible in what it would have done: treated a read error as the end of input and truncated someone's data (§14).

Capability io; refused at comptime (D33). Delegates to the prelude's read_line, once per line.

(io.input_all() else "?") == ""

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: eof, io, utf8. Their conditions are documented on the functions that return them.

No public declarations in this section.