▼ |  | Unstable: May Change Without Warning

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

On this page:

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

top ← prev up next →

### 30`` 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 →