▼ |  | Unstable: May Change Without Warning

1 | Guidelines for Developing unstable Libraries
2 | Automata: Compiling State Machines
3 | Bytes
4 | Contracts
5 | Contracts for Macro Subexpressions
6 | Debugging
7 | Definitions
8 | Errors
9 | Filesystem
10 | Find
11 | Flonums
12 | Futures
13 | Functions
14 | Hash Tables
15 | Interface-Oriented Programming for Classes
16 | Lazy Require
17 | Lists
18 | Logging
19 | Macro Testing
20 | Mark Parameters
21 | Match
22 | Open place expressions
23 | Options
24 | Parameter Groups
25 | Pretty-Printing
26 | Re-Contracting Identifiers
27 | Sandbox
28 | Sequences
29 | Strings
30 | Structs
31 | Struct Printing
32 | Syntax
33 | Temporal Contracts: Explicit Contract Monitors
34 | Unix Domain Sockets
35 | 2D Syntax
36 | GUI Libraries

On this page:

make-constructor-style-printer
prop: auto-custom-write

top ← prev up next →

### 31`` 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)

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

> > value

> > prop:auto-custom-write : (struct-type-property/c 'constructor)

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>

top ← prev up next →