By default structures are printed like
#<struct:s> by the REPL,
display and so on. You want to see the contents of structures when they are printed.
There are two steps to getting the contents of structures printed. You must:
- Make the structure transparent so other code can inspect its content
- Tell the default printer to print out the contents of transparent structures
To do the first you must pass an inspector to the call of
define-struct like so:
(define-struct foo (field1 field2) (make-inspector))
(define-struct foo (field1 field2) #f)
Then you must set the
print-struct parameter to
#t, like so:
(print-struct #t)
(parameterize
((print-struct #t))
expr ...)
Here's a complete example:
> (define-struct opaque (a b))
> (define-struct transparent (a b) #f)
> (make-opaque 1 2)
#<struct:opaque>
> (make-transparent 1 2)
#<struct:transparent>
> (print-struct #t)
> (make-opaque 1 2)
#<struct:opaque>
> (make-transparent 1 2)
#3(struct:transparent 1 2)
The output produced by the REPL,
display, and most other high-level output functions is controlled be a function known as the default print handler. Its behaviour can be customised by a number of parameters. We've already seen on such parameter: =print-struct+. There are many more, such as =print-hash-table. See the
PltScheme documentation for more information on these parameters.
Additionally, the default print handler can be changed globally, or on a port by port basis. This allows output to be completely customised, or changed for specified ports. See the documentation for
current-print, and
port-print-handler.
Inspectors are used to control access to structure information and to module bindings. When we pass an inspector to
define-struct, it isn't actually that inspector that control access, but the parent of that inspector. We can provide a parent explicitly, like so:
(make-inspector parent). If we don't provide a parent the value of the
current-inspector parameter is taken. Note there is a slight difference between passing an inspector and
#f to
define-struct. The former gives control to the inspector's parent, while the latter make the structure transparent to all code. This subtle difference doesn't mean much in most cases, but could lead to differences when making advanced uses of inspectors.
--
NoelWelsh - 20 Apr 2006