s c h e m a t i c s : c o o k b o o k

/ Cookbook.StructurePrinting

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Printing Structures

Problem

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.

Solution

There are two steps to getting the contents of structures printed. You must:

  1. Make the structure transparent so other code can inspect its content
  2. 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:

;; Do this
(define-struct foo (field1 field2) (make-inspector))
;; Or do this
(define-struct foo (field1 field2) #f)

Then you must set the print-struct parameter to #t, like so:

;; Either set the parameter globablly
(print-struct #t)
;; Or set it in the scope of some expressions
(parameterize
  ((print-struct #t))
  expr ...)

Here's a complete example:

;; opaque is opaque, transparent is transparent
> (define-struct opaque (a b))          
> (define-struct transparent (a b) #f)
;; but both aren't printed out with the default setting on print-struct
> (make-opaque 1 2)
#<struct:opaque>
> (make-transparent 1 2)
#<struct:transparent>
;; we change print-struct
> (print-struct #t)
> (make-opaque 1 2)
#<struct:opaque>
;; and now the contents of transparent structures is printed
> (make-transparent 1 2)
#3(struct:transparent 1 2)

Discussion

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.


Comments about this recipe

Contributors

-- NoelWelsh - 20 Apr 2006

CookbookForm
TopicType: Recipe
ParentTopic: StructureRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest