From: Nathan Sidwell
Subject: defstruct & :print-function
Date: 
Message-ID: <329304C6.2C9B@pact.srf.ac.uk>
Hi,

I'm supplying my own print function for some structures
in the defstruct declaration. This all works well, however
I'm currently ignoring the value of *print-readably*.

The behaviour I desire is that if *print-readably* is true
then the structure is printed in the normal #s() notation, and
if it's false then use my own format. However I can't find an
easy way of getting at the normal structure printing function
or mechanism. The only way I know of would be to explicitly
access each structure member like
	(format stream "~s ~s" :member (struct-type-member object))
which is clearly teduious and error prone. Is there a
way to iterate though the structure members?
Not being able to print the structures readably is making debugging
awkward.

I'm also a little confused about how I should be handling the
depth parameter passed to the print function. Do I ignore this
or do I have to compare it against *print-depth*, if I'm about
to print a list-like object?

thanks,
nathan
-- 
Nathan Sidwell                    The windy road is more interesting
Chameleon Architecture Group at SGS-Thomson, formerly Inmos
http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
······@pact.srf.ac.uk or ······@inmos.co.uk or ······@bristol.st.com
From: Howard R. Stearns
Subject: Re: defstruct & :print-function
Date: 
Message-ID: <329321A7.15FB7483@elwoodcorp.com>
Nathan Sidwell wrote:
> 
> Hi,
> 
> I'm supplying my own print function for some structures
> in the defstruct declaration. This all works well, however
> I'm currently ignoring the value of *print-readably*.
> 
> The behaviour I desire is that if *print-readably* is true
> then the structure is printed in the normal #s() notation, and
> if it's false then use my own format. However I can't find an
> easy way of getting at the normal structure printing function
> or mechanism. The only way I know of would be to explicitly
> access each structure member like
>         (format stream "~s ~s" :member (struct-type-member object))
> which is clearly teduious and error prone. Is there a
> way to iterate though the structure members?
> Not being able to print the structures readably is making debugging
> awkward.
> 

From the ANSI definition of defstruct:

 If neither a :print-function nor a :print-object option is supplied,  
 then defstruct does not generate a print-object method specialized for 
 structure-name and some default behavior is inherited either from a 
 structure named in an :include option or from the default behavior for 
 printing structures; see the function print-object and Section 
 22.1.3.12 (Printing Structures). 

You should be able to define PRINT-OBJECT method (using DEFMETHOD,) that
uses CALL-NEXT-METHOD to access the basic #S() printer.  If you :INCLUDE
a structure that uses :PRINT-FUNCTION or :PRINT-OBJECT, then this won't
work, but you can work around it knowing that:

 if either a :print-function or a :print-object option is supplied, and 
 if no printer-name is supplied, then a print-object method specialized 
 for structure-name is generated that calls a function that implements 
 the default printing behavior for structures using #S notation; see 
 Section 22.1.3.12 (Printing Structures). 

As a separate issue, if you want to get a hold of the structure slot
names and values the MetaObjectProtocol used by SOME implementations
allows you to use (mapcar #'slot-definition-name (class-slots (class-of
<structure-instance>))) and (slot-value <structure-instance>
<slot-name>)

> I'm also a little confused about how I should be handling the
> depth parameter passed to the print function. Do I ignore this
> or do I have to compare it against *print-depth*, if I'm about
> to print a list-like object?

This is supposed to happen for you automagically if you use write,
print, or format to display internal components directly to the stream.
This WILL NOT work if you format them to individual strings first, and
then just output the string.

> 
> thanks,
> nathan
> --
> Nathan Sidwell                    The windy road is more interesting
> Chameleon Architecture Group at SGS-Thomson, formerly Inmos
> http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
> ······@pact.srf.ac.uk or ······@inmos.co.uk or ······@bristol.st.com