On this page:
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.15.0.12

9.6 Printables🔗ℹ

Any value is printable. Implementing the Printable interface customizes the way that instances of a class print.

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

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