(rontolisp) docs
← Macros

with-output-to-string

(with-output-to-string (stream &optional string &key element-type) body...)

Binds stream to a string output stream, evaluates the body forms, and returns everything written to the stream as a string. princ, prin1, print, terpri, fresh-line, write-line, write-char and write-string accept the stream as their optional stream argument, and format accepts it as the destination; each call appends to the stream. Works in all three backends.

Given a string with a fill pointer, the output is appended to it through the fill pointer instead, and the form returns the values of the last body form. The string receives the output when the body exits, not character by character. :element-type is accepted and has no effect: every stream here is a character stream.

Naming the bound variable *standard-output* redirects the whole stream-argument-less print family for the extent of the body -- including inside called functions, and including format with the t destination -- because those calls read the current (dynamically bound) value of *standard-output* at call time. The same redirect works for any let that binds *standard-output* to an output stream.

A nil stream argument means the same thing as an omitted one -- it is the *standard-output* designator, not "raw standard output". That is what makes the common Common Lisp shape of a renderer forwarding its own optional argument work under the redirect:

Binding *error-output* the same way captures the reports of warn instead; that variable's default is the process standard error, so an unredirected warning never lands on standard output.