[plt-scheme] Printing Structures without using custom-write

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Oct 10 16:46:02 EDT 2007

On Oct 10, Sam TH wrote:
> On 10/10/07, Eli Barzilay <eli at barzilay.org> wrote:
> > On Oct 10, Sam TH wrote:
> > > On 10/10/07, Eli Barzilay <eli at barzilay.org> wrote:
> > > > On Oct 10, Sam TH wrote:
> > > > > On 10/10/07, Eli Barzilay <eli at barzilay.org> wrote:
> > > > > > On Oct 10, Sam TH wrote:
> > > > > > > I have this already, because it would be very hard to some things
> > > > > > > otherwise.  But it's not a total solution - you can't debug your write
> > > > > > > handler with printf, because you immediately get an infinite loop.
> > > > > > >
> > > > > > > Also, it requires having all your structures defined with a macro that
> > > > > > > expands to define-struct, which is the case for me, but limits the
> > > > > > > applicability of the technique.
> > > > > >
> > > > > > Can't you have a parameter that conrols your own output, and based on
> > > > > > that your printers will spit out the nice output or something that
> > > > > > looks like the standard output?
> > > > >
> > > > > Yes, but then you have to re-implement the standard printer.
> > > >
> > > > ...which is a one liner, so it shouldn't be a problem in practice.
> > >
> > > This is false.  It's one line per structure, which is significant for
> > > any complex set of structs.
> >
> > (define simple-output (make-parameter #f))
> > (define (printer-wrapper printer)
> >   (lambda (obj port write?)
> >     (if (simple-output)
> >       ((if write? write display) (struct->vector obj) port)
> >       (printer obj port write?))))
> >
> > ...And all you need is to wrap your printers.
> 
> And now we've reimplemented the MzScheme behavior.

But you *have* the MzScheme default behavior -- just write the
struct->vector out, that's all.  It sounds like you'd be happy with
some `struct->defaultly-printed-value' -- and that's what you have,
except that it's called `struct->vector'...


> Which we will have to fix when it changes.

That was my point that you said you didn't understand:

  > > Conceptually, there may be future extensions to struct printing
  > > that you'll want to have -- but even with that I think that if
  > > you want your own output then you should go all the way with
  > > that.
  >
  > I don't even understand what you're suggesting here.


> This is the same problem as the Stepper, on a much smaller scale.
> Reimplementing the behavior of the implementation is a bad idea.  I
> don't feel like this should be controversial.

IIUC, you're arguing for including a

  (define struct->defaultly-printed-value struct->vector)

or equivalently,

  (define (default-struct-printer obj port write?)
    ((if write? write display) obj port write?))

*in* mzscheme right now; which can change if the default printer
changes, right?  If so, then I see value in it only if the default
printer is likely to change to something meaningful (not like the
current rough-information version).

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.