On this page:
make-constructor-style-printer
prop:  auto-custom-write

28 Struct Printing

Ryan Culpepper <ryanc@racket-lang.org>

This library is unstable; compatibility will not be maintained. See Unstable: May Change Without Warning for more information.

 (require unstable/custom-write) package: base

procedure

(make-constructor-style-printer get-constructor 
  get-contents) 
  (-> any/c output-port? (or/c #t #f 0 1) void?)
  get-constructor : (-> any/c (or/c symbol? string?))
  get-contents : (-> any/c sequence?)
Produces a function suitable as a value for prop:custom-write. The function prints values in “constructor style.” When the value is printed as an expression, it is shown as an application of the constructor (as returned by get-constructor) to the contents (as returned by get-contents). When given to write, it is shown as an unreadable value with the constructor separated from the contents by a colon.

Examples:

> (struct point (x y)
    #:property prop:custom-write
    (make-constructor-style-printer
     (lambda (obj) 'point)
     (lambda (obj) (list (point-x obj) (point-y obj)))))
> (print (point 1 2))

(point 1 2)

> (write (point 1 2))

#<point: 1 2>

The function also cooperates with pretty-print:

Examples:

> (parameterize ((pretty-print-columns 10))
    (pretty-print (point 3000000 4000000)))

(point

 3000000

 4000000)

> (parameterize ((pretty-print-columns 10))
    (pretty-write (point 3000000 4000000)))

#<point:

 3000000

 4000000>

When attached to a struct type, automatically generates a printer using make-constructor-style-printer and attaches it to the struct type’s prop:custom-write property. It also sets the prop:custom-print-quotable property to 'never.

Examples:

> (struct point3 (x y z)
    #:property prop:auto-custom-write 'constructor)
> (print (point3 3 4 5))

(point3 3 4 5)

> (write (point3 3 4 5))

#<point3: 3 4 5>