6.50 Input and Output
A port is an input or output stream for a file, network connection, terminal, etc. An input port is specifically for input, while an output port is specifically for output.
out.get_bytes()
is
out.get_string()
is
out.flush()
is
Port.Output.flush(out)
annotation |
|
annotation |
|
annotation |
function | |||||
|
In #'text mode, strings, symbols, identifiers, and keywords print as their character content, a byte string prints as its raw byte content, and a syntax object prints as unquoted. Any other predefined kind of value prints the same in #'text and #'expr mode, but a class can implement Printable so that its instances print differently in different modes.
When pretty is #false, then printing tends to use a single line, but also prints faster. The value of the Printable.current_pretty context parameter is to match pretty while printing, which affects functions like PrintDesc.list.
function | |||||
|
context parameter |
context parameter |
context parameter |
function |
|
function |
Port.Output.open_string does the same as Port.Output.open_bytes, but can be used to clarify the intention together with Port.Output.get_string.
function |
|
function |
Port.Output.get_string is like Port.Output.get_bytes, but returns a string converted from the byte string instead.
function | ||
|
An interface that a class can implement (publicly or privately) to customize the way its objects print. In the simplest case, an implementation of the interface’s describe method returns a string to use as an object’s printed form. More generally, a describe method implementation returns a description of how to print a value, where the description is created using functions like PrintDesc.concat, PrintDesc.newline, and PrintDesc.or.
The Printable interface has one method:
describe(mode, recur) —
returns a PrintDesc given a mode, which is either #'text or #'expr, and a recur function, which accepts a value and an optional ~mode like Printable.describe (unlike Printable.describe, the ~mode defaults to #'expr, which is generally desirable when printing subcomponents); the recur function is specific to a particular overall print action so that it can handle cycles and graph references.
function | ||||
function | |||||
|
The optional column argument indicates the current column for output, in case pd contains a PrintDesc.align description that needs to update indentation based on the current column.
annotation |
A string or byte string prints as its content. Other PrintDesc values describe concatenations, line breaks, indentation, and formatting options.
function | ||
| ||
| ||
function | ||
| ||
function | ||
| ||
function | ||
| ||
function | ||
| ||
function | ||
|
PrintDesc.concat concatenates the pds in order, with nothing in between.
"a", "b",
))
ab
PrintDesc.newline prints a newline plus indentation, where indentation is determined by the surrounding description.
"a", PrintDesc.newline(),
"b",
))
a
b
PrintDesc.nest increases the current indentation by n while printing pd.
"a",
2,
"b",
)),
))
a
b
PrintDesc.align sets the current indentation (independent of the current indentation) to the current output column while printing pd.
"a",
"b", PrintDesc.newline(),
"c",
)),
))
ab
c
PrintDesc.or offers two printign alternatives. Either pd1 or pd2 will be printed, depending on choices made by a pretty-printer configuration and as constrainted by PrintDesc.flat constraints.
"a", "; ", "b",
),
"a", PrintDesc.newline(),
"b",
)))
a; b
PrintDesc.flat prints the same as pd, but only if that is possible without any newlines. If all possible ways of rendering pd involve a newline, printing fails. A PrintDesc.flat constraint it particularly useful in one branch of a PrintDesc.or to constrain a description received by recursive description.
> def sub = PrintDesc.or(
"a", "; ", "b",
),
"a", PrintDesc.newline(),
"b",
))
"f(", PrintDesc.flat(sub), ")",
),
"f(",
2,
sub,
)),
")",
)))
f(a; b)
function | |||||
| |||||
| |||||
function | |||||
The single-line variant constrains pre_pd, head, and any member of elements other than the last one to be printed as a single-line, too. If one of those has no single-line option, then the combined single-line variant will not be used (which can cause the description to be unprintable).
"Posn(",
["x", "y"],
")"
))
Posn(x, y)
"begin",
"one", PrintDesc.newline(),
"two",
)))
begin:
one
two
context parameter | ||
|
context parameter | ||
|
context parameter | ||
|
context parameter | ||
|
Sharing is reported by a #n= prefix on a printed value, and then #n# with the same number n is used for later occurrences of the value.
The same notation is used to show cyclic data, which is shown independent of the value of the Printable.current_graph parameter.