*print-readably* and structures 
Author Message
 *print-readably* and structures

Consider the following structure:

(defstruct (point
            (:constructor make-point (x y z &optional (w 1.0))))
  (x 0.0)
  (y 0.0)
  (z 0.0)
  (w 0.0))

Is it reasonable to expect the printer to signal an error
when printing an instance of this structure when *print-readably* is t?
Objects like #s(point :x 0 :y 0 :z 0 :w 1) can't be read with
the above structure definition.

The HyperSpec enabled me to figure out how to use the #s syntax
and still have the boa constructor, too.  Neither Graham's ANSI
Common Lisp nor CLTL2 (which is out of date, especially on this
topic) say how to do this. Yay for the HyperSpec!

(defstruct (point
            (:constructor new-point (x y z &optional (w 1.0)))
            (:constructor))
  (x 0.0)
  (y 0.0)
  (z 0.0)
  (w 0.0))

-matt



Sat, 05 Jan 2002 03:00:00 GMT  
 *print-readably* and structures

Quote:
> Is it reasonable to expect the printer to signal an error
> when printing an instance of this structure when *print-readably* is t?
> Objects like #s(point :x 0 :y 0 :z 0 :w 1) can't be read with
> the above structure definition.

No.  I think you should assume that you shouldn't be printing anything
and expecting it to be re-readable unless the same definitions are
loaded.  For example, you are obliged to have the same package structure
and you won't get an error if you try to print a package that might not
be loaded at re-read time.  The burden is on you to make sure a compatible
environment is in place for the re-read.  *print-readably* is only supposed
to address the issue of whether that same compatible environment contained
no way to invert the output rep.

I'm not sure it says this anywhere in the spec.  But rationality kind of
implies it and I bet (without checking) you'll find all implementations
make the assumption I just said.  I would be very surprised if any
implementation signaled an error in the case you describe, and if it did
I would send a bug report.



Sun, 06 Jan 2002 03:00:00 GMT  
 *print-readably* and structures

Quote:

> > Is it reasonable to expect the printer to signal an error
> > when printing an instance of this structure when *print-readably* is t?
> > Objects like #s(point :x 0 :y 0 :z 0 :w 1) can't be read with
> > the above structure definition.

> No.  I think you should assume that you shouldn't be printing anything
> and expecting it to be re-readable unless the same definitions are
> loaded.

Kent, I think you misread him: he said "with", not "without".  The
reason #S(point ...) can't read with that structure definition is that
it didn't include a standard constructor function, as required by ANS
2.4.8.13:

Quote:
> > (defstruct (point
> >             (:constructor make-point (x y z &optional (w 1.0))))
> >   (x 0.0)
> >   (y 0.0)
> >   (z 0.0)
> >   (w 0.0))

I'd be inclined to say the printer should signal an error: Stricly
speaking, that structure can't be printed readably, since the
definition of "readably" involves similarity, and similarity is not
defined on a structure type (ANS 3.2.4.2.2), unless you define a
MAKE-LOAD-FORM method for it.  Even if "readably" was extended with a
special case for #S, it would not be too much to ask for the printer
to check that the required constructor exists.

Now, if there was a MAKE-LOAD-FORM method, the printer could call it
and construct a readable representation using #. on the results (the
details are a little complicated).  There's plenty of precedent for
*PRINT-READABLY* changing the behaviour of the printer.

However, it is not reasonable to expect the printer in the current
implementations to signal an error.  I doubt many implementors have
worked out the somewhat counterintuitive implications of these
definitions.
--
Pekka P. Pirinen
(the new) Harlequin Limited
All that glitters has a high refractive index.



Sun, 06 Jan 2002 03:00:00 GMT  
 *print-readably* and structures

Quote:
> Kent, I think you misread him: he said "with", not "without".

So I did.

Quote:
> I'd be inclined to say the printer should signal an error:

Yeah, it does seem in this case that it's not an unreasonable
thing to do.  I don't think it's required, only because of how the
spec is worded.  But nothing keeps a vendor from "helping".
I'd have to think a lot to be absolutely sure, but it *sounds* helpful.

Quote:
> Now, if there was a MAKE-LOAD-FORM method, the printer could call it
> and construct a readable representation using #. on the results

Yeah, I do that sometimes...


Sun, 06 Jan 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. *print-readably* and printing numbers

2. (write 1f0 :readably t) prints what?

3. printing arrays readably

4. *print-readably* and hash tables

5. princ & *print-readably*

6. *print-readably* and functions

7. How do I suppress a group header in a print structure

8. Datafiles structures, Printing

9. How can I print my structure?

10. Print a structure of Clipper Datavase

11. reading/printing structures

12. reading/printing structures

 

 
Powered by phpBB® Forum Software