6.52 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.
is
Port.Input.peek_byte(in, arg, ...)
is
Port.Input.peek_bytes(in, arg, ...)
is
Port.Input.peek_char(in, arg, ...)
is
Port.Input.peek_string(in, arg, ...)
in.read_byte()
is
is
Port.Input.read_bytes(in, arg, ...)
in.read_char()
is
is
Port.Input.read_line(in, arg, ...)
in.read_string(arg)
is
Port.Input.read_string(in, arg)
out.get_bytes()
is
out.get_string()
is
out.flush()
is
Port.Output.flush(out)
is
Port.Output.print(out, arg, ...)
is
Port.Output.println(out, arg, ...)
is
Port.Output.show(out, arg, ...)
is
Port.Output.showln(out, arg, ...)
annotation | |
| |
annotation | |
| |
annotation | |
| |
annotation | |
The Port.EOF annotation is satisfied by the Port.eof value.
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 #true, then compound values like lists may print with line breaks to split the output across lines. 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.
> print("apple")
apple
"apple"
> print("apple", "banana", "coconut")
apple banana coconut
> print("apple", "banana", "coconut", ~pretty: #true)
apple
banana
coconut
function | |||||
|
function | ||||
| ||||
| ||||
function | ||||
|
context parameter | |
context parameter | |
context parameter | |
function | |||
|
function | |||
function | |||
function | ||||
function | |||
function | ||||
function | ||
function | |||
function | ||
function | |||
|
Characters are read from in until a line separator or an end-of-file is read. The line separator is not included in the result string (but it is removed from the port’s stream). If no characters are read before an end-of-file is encountered, Port.eof is returned.
The mode argument determines the line separator(s). It must be one of the following symbols:
#'linefeed breaks lines on linefeed characters.
#'return breaks lines on return characters.
#'return_linefeed breaks lines on return-linefeed combinations. If a return character is not followed by a linefeed character, it is included in the result string; similarly, a linefeed that is not preceded by a return is included in the result string.
#'any breaks lines on any of a return character, linefeed character, or return-linefeed combination. If a return character is followed by a linefeed character, the two are treated as a combination.
#'any_one breaks lines on either a return or linefeed character, without recognizing return-linefeed combinations.
function | |||
If amount is 0, then the empty string is returned. Otherwise, if fewer than amount characters are available before an end-of-file is encountered, then the returned string will contain only those characters before the end-of-file; that is, the returned string’s length will be less than amount. (A temporary string of size amount is allocated while reading the input, even if the size of the result is less than amount characters.) If no characters are available before an end-of-file, then Port.eof is returned.
enumeration | ||||||
function | ||||
| ||||
function | ||||
| ||||
function | ||||
| ||||
| ||||
function | ||||
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 | |||||
|
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
enumeration | |||||
function | ||||||
|
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.