On this page:
Port
Port.Input
Port.Output
Port.EOF
print
println
show
showln
Print  Mode
Print  Mode.text
Print  Mode.expr
Port.Input.current
Port.Output.current
Port.eof
Port.Output.current_  error
Port.Input.open_  bytes
Port.Input.open_  string
Port.Input.peek_  byte
Port.Input.peek_  bytes
Port.Input.peek_  char
Port.Input.peek_  string
Port.Input.read_  byte
Port.Input.read_  bytes
Port.Input.read_  char
Port.Input.read_  line
Port.Input.read_  string
Port.Read  Line  Mode
Port.Read  Line  Mode.linefeed
Port.Read  Line  Mode.return
Port.Read  Line  Mode.return_  linefeed
Port.Read  Line  Mode.any
Port.Read  Line  Mode.any_  one
Port.Output.print
Port.Output.println
Port.Output.show
Port.Output.showln
Port.Output.open_  bytes
Port.Output.open_  string
Port.Output.get_  bytes
Port.Output.get_  string
Port.Output.flush
Printable
Printable.describe
Printable.render
Print  Desc
Print  Desc.concat
Print  Desc.newline
Print  Desc.nest
Print  Desc.align
Print  Desc.or
Print  Desc.flat
Print  Desc.list
Print  Desc.block
Print  Desc.Special  Mode
Print  Desc.Special  Mode.write_  special
Print  Desc.Special  Mode.print
Print  Desc.Special  Mode.write
Print  Desc.Special  Mode.display
Print  Desc.special
Printable.current_  pretty
Printable.current_  optimal
Printable.current_  page_  width
Printable.current_  graph
8.14.0.2

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.

The . operator can be used on a input port expression as equivalent to calling Port.Input functions:

in.peek_byte(arg, ...)

 is 

Port.Input.peek_byte(in, arg, ...)

in.peek_bytes(arg, ...)

 is 

Port.Input.peek_bytes(in, arg, ...)

in.peek_char(arg, ...)

 is 

Port.Input.peek_char(in, arg, ...)

in.peek_string(arg, ...)

 is 

Port.Input.peek_string(in, arg, ...)

in.read_byte()

 is 

Port.Input.read_byte(in)

in.read_bytes(arg, ...)

 is 

Port.Input.read_bytes(in, arg, ...)

in.read_char()

 is 

Port.Input.read_char(in)

in.read_line(arg, ...)

 is 

Port.Input.read_line(in, arg, ...)

in.read_string(arg)

 is 

Port.Input.read_string(in, arg)

The . operator can be used on a output port expression as equivalent to calling Port.Output functions:

out.get_bytes()

 is 

Port.Output.get_bytes(out)

out.get_string()

 is 

Port.Output.get_string(out)

out.flush()

 is 

Port.Output.flush(out)

out.print(arg, ...)

 is 

Port.Output.print(out, arg, ...)

out.println(arg, ...)

 is 

Port.Output.println(out, arg, ...)

out.show(arg, ...)

 is 

Port.Output.show(out, arg, ...)

out.showln(arg, ...)

 is 

Port.Output.showln(out, arg, ...)

annotation

Port

 

annotation

Port.Input

 

annotation

Port.Output

 

annotation

Port.EOF

The Port annotation is satisified by a port. The Port.Input annotation recognizes input ports specifically, while Port.Output recognizes output ports, and it is possible for a port to be both.

The Port.EOF annotation is satisfied by the Port.eof value.

function

fun print(v :: Any, ...,

          ~out: out :: Port.Output = Port.Output.current(),

          ~mode: mode :: PrintMode = #'text,

          ~pretty: pretty = Printable.current_pretty())

  :: Void

Prints each v to out. In the case that more than one v is provided, a space is printed between the output for each vunless pretty is #true, in which case a newline is printed between the output of each v.

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

> print("apple", ~mode: #'expr)

"apple"

> print("apple", "banana", "coconut")

apple banana coconut

> print("apple", "banana", "coconut", ~pretty: #true)

apple

banana

coconut

function

fun println(v :: Any, ...,

            ~out: out :: Port.Output = Port.Output.current(),

            ~mode: mode :: PrintMode = #'text,

            ~pretty: pretty = Printable.current_pretty())

  :: Void

Prints like print, then prints a newline.

function

fun show(v :: Any, ...,

         ~out: out :: Port.Output = Port.Output.current(),

         ~pretty: pretty = Printable.current_pretty())

  :: Void

 

function

fun showln(v :: Any, ...,

           ~out: out :: Port.Output = Port.Output.current(),

           ~pretty: pretty = Printable.current_pretty())

  :: Void

Like print and println with ~mode: #'expr.

enumeration

enum PrintMode:

  text

  expr

A printing mode for use with functions like print.

A context parameter for the default port to use when reading.

A context parameter for the default port to use when printing.

A value (distinct from all other values) that represents an end-of-file.

A context parameter for the default port to use when printing errors.

function

fun Port.Input.open_bytes(bstr :: Bytes,

                          name :: Symbol = #'string)

  :: Port.Input

Creates an input port that reads bytes from bstr, a byte string. The optional name is used as the name for the returned port.

function

fun Port.Input.open_string(str :: ReadableString,

                           name :: Symbol = #'string)

  :: Port.Input

Creates an input port that reads characters from str, a string. The optional name is used as the name for the returned port.

function

fun Port.Input.peek_byte(in :: Port.Input,

                         ~skip_bytes: skip :: NonnegInt = 0)

  :: Byte || Port.EOF

Like Port.Input.read_byte, but peeks instead of reading, and skips skip bytes at the start of the port.

function

fun Port.Input.peek_bytes(in :: Port.Input,

                          amount :: NonnegInt,

                          ~skip_bytes: skip :: NonnegInt = 0)

  :: Bytes || Port.EOF

Like Port.Input.read_bytes, but peeks instead of reading, and skips skip bytes at the start of the port.

function

fun Port.Input.peek_char(in :: Port.Input,

                         ~skip_bytes: skip :: NonnegInt = 0)

  :: Char || Port.EOF

Like Port.Input.read_char, but peeks instead of reading, and skips skip bytes (not characters) at the start of the port.

function

fun Port.Input.peek_string(in :: Port.Input,

                           amount :: NonnegInt,

                           ~skip_bytes: skip :: NonnegInt = 0)

  :: String || Port.EOF

Like Port.Input.read_string, but peeks instead of reading, and skips skip bytes at the start of the port.

Reads a single byte from in. If no bytes are available before and end-of-file, then Port.eof is returned.

function

fun Port.Input.read_bytes(in :: Port.Input,

                          amount :: NonnegInt)

  :: Bytes || Port.EOF

Reads a byte string containing the next amount bytes from in. If amount is 0, then an empty byte string is returned. Otherwise if fewer than amount bytes are available before an end-of-file is encountered, then the returned byte string will contain only those bytes before the end-of-file; that is, the returned byte string’s length will be less than amount. If no bytes are available before an end-of-file, then Port.eof is returned.

Reads a single character from in which may involve reading several bytes to UTF-8-decode them into a character; a minimal number of bytes are read/peeked to perform the decoding. If no bytes are available before an end-of-file, then Port.eof is returned.

function

fun Port.Input.read_line(in :: Port.Input,

                         ~mode: mode :: Port.ReadLineMode = #'any)

  :: String || Port.EOF

Returns a string containing the next line of characters from in.

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:

function

fun Port.Input.read_string(in :: Port.Input,

                           amount :: NonnegInt)

  :: String || Port.EOF

Returns a string containing the next amount characters from in.

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.

Line reading modes for Port.Input.read_line.

function

fun Port.Output.print(out :: Port.Output,

                      v :: Any, ...,

                      ~mode: mode :: PrintMode = #'text)

  :: Void

 

function

fun Port.Output.println(out :: Port.Output,

                        v :: Any, ...,

                        ~mode: mode :: PrintMode = #'text)

  :: Void

 

function

fun Port.Output.show(out :: Port.Output,

                     v :: Any, ...,

                     ~mode: mode :: PrintMode = #'text)

  :: Void

 

function

fun Port.Output.showln(out :: Port.Output,

                       v :: Any, ...,

                       ~mode: mode :: PrintMode = #'text)

  :: Void

The same as print, println, show, and showln, but with an output provided as a required intiial argument instead of an optional ~out keyword argument.

Creates an output port that accumulates the output into a byte string. The optional name argument is used as the name for the returned port.

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.

Port.Output.get_bytes returns the bytes accumulated in the output port out so far in a freshly allocated byte string (including any bytes written after the port’s current position, if any).

Port.Output.get_string is like Port.Output.get_bytes, but returns a string converted from the byte string instead.

Flushes the content of out’s buffer.

Provided only in the class space, not the annot or namespace space.

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:

function

fun Printable.describe(

  v :: Any,

  ~mode: mode :: PrintMode = #'text

) :: PrintDesc

Generates a pretty-printing description for v. The print function composes Printable.describe with Printable.render.

function

fun Printable.render(

  pd :: PrintDesc,

  out :: Port.Output = Port.Output.current(),

  ~column: column :: NonnegInt = 0

) :: Void

Pretty-prints the description pd to out.

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

PrintDesc

Satisified by a string, byte string, or opaque result returned by functions like PrintDesc.concat.

A string or byte string prints as its content. Other PrintDesc values describe concatenations, line breaks, indentation, and formatting options.

function

fun PrintDesc.concat(pd :: PrintDesc, ...)

  :: PrintDesc

 

function

fun PrintDesc.newline()

  :: PrintDesc

 

function

fun PrintDesc.nest(n :: NonnegInt, pd :: PrintDesc)

  :: PrintDesc

 

function

fun PrintDesc.align(pd :: PrintDesc)

  :: PrintDesc

 

function

fun PrintDesc.or(pd1 :: PrintDesc, pd2 :: PrintDesc)

  :: PrintDesc

 

function

fun PrintDesc.flat(pd :: PrintDesc)

  :: PrintDesc

Core PrintDesc constructors (in addition to plain strings and byte strings):

function

fun PrintDesc.list(

  pre_pd :: PrintDesc,

  elements :: Listable.to_list && List.of(PrintDesc),

  post_pd :: PrintDesc

) :: PrintDesc

 

function

fun PrintDesc.block(

  head_pd :: PrintDesc,

  body :: PrintDesc

) :: PrintDesc

Description-building helpers for list-like and block-like forms where the printing options include a single-variant and multi-line variants, but the latter only when Printable.current_pretty is set to #true.

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).

> Printable.render(

    PrintDesc.list(

      "Posn(",

      ["x", "y"],

      ")"

    ))

Posn(x, y)

> Printable.render(

    PrintDesc.block(

      "begin",

      PrintDesc.concat(

        "one", PrintDesc.newline(),

        "two",

      )))

begin:

  one

  two

function

fun PrintDesc.special(v :: Any,

                      alt_pd :: PrintDesc,

                      ~length: length :: NonnegInt = 1,

                      ~mode: mode :: PrintDesc.SpecialMode

                               = #'write_special)

  :: PrintDesc

Prints v using Racket printing when the output port supports “special” output, otherwise prints as the given alt_pd. For the purposes of pretty printing, v is counted as using length columns. The mode argument indicates which Racket printing function is used.

A context parameter that determines the default printing mode. The parameter’s value is used by print, println, and PrintDesc.list, for example.

A context parameter that determines whether pretty printing uses a faster but non-optimal strategy or a slower, optimal strategy. The parameter’s value is not used when Printable.current_pretty is #false.

A context parameter for pretty printing that determines the maximum number of columns that printing should use, if possible.

A context parameter that determines whether printing shows sharing of objects in terms of === identity.

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.