On this page:
Port
Port.Input
Port.Output
print
println
Port.Input.current
Port.Output.current
Port.Output.current_  error
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
Printable.current_  pretty
Printable.current_  optimal
Printable.current_  page_  width
Printable.current_  graph
8.12

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.

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)

annotation

Port

 

annotation

Port.Input

 

annotation

Port.Output

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.

function

fun print(v :: Any,

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

          ~mode: mode :: Any.of(#'text, #'expr) = #'text,

          ~pretty: pretty = Printable.current_pretty())

  :: Void

Prints v to out.

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

fun println(v :: Any,

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

            ~mode: mode :: Any.of(#'text, #'expr) = #'text,

            ~pretty: pretty = Printable.current_pretty())

  :: Void

Prints like print, then prints a newline.

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

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

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

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 :: Any.of(#'text, #'expr) = #'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 :: Any.of(#'#{write-special},

                                            #'print,

                                            #'write,

                                            #'display)

                               = #'#{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.