▼ |  | 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 | Futures
10 | Functions
11 | Hash Tables
12 | Interface-Oriented Programming for Classes
13 | Lazy Require
14 | Lists
15 | Logging
16 | Macro Testing
17 | Mark Parameters
18 | Match
19 | Open place expressions
20 | Option Contracts
21 | Parameter Groups
22 | Pretty-Printing
23 | Re-Contracting Identifiers
24 | Sandbox
25 | Sequences
26 | Strings
27 | Structs
28 | Struct Printing
29 | Syntax
30 | Temporal Contracts: Explicit Contract Monitors
31 | Unix Domain Sockets
32 | 2D Syntax

On this page:

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

top ← prev up next →

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

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