[gambit-list] Limiting object external representation

Marc Feeley feeley at iro.umontreal.ca
Thu Sep 6 11:46:35 EDT 2007


On 26-Aug-07, at 11:29 PM, |/|/ Bendick wrote:

> The readtables doc is a bit over my head. Do readtables allow me to
> define how the system will display a type I create? For example,
> something like:
>
>> (set 1 2 'pi)
> {1 2 pi}
>
>> (dict 'grass 'green 'sky 'blue 'v8 'red)
> [grass => green sky => blue v8 => red]
>
> If not, is such a thing possible through some other means, in gambit?

No readtables do not support this currently.  I think they should but  
it is not clear to me how this should be done.

Currently the only hook for the "printer" is the procedure ##wr which  
can be redefined.  ##wr receives two parameters: we (the write  
environment) and obj (the object to write).  The write environment is  
a structure containing a flag indicating the writing style (display,  
write, pretty-print), the output port, the readtable, etc.  The  
procedure ##wr-str can be used to send strings to the write  
environment.  Here's an example:

(define-type point x y)
(define-type rect p1 p2)

(set! ##wr
       (let ((old-wr ##wr))
         (lambda (we obj)
           (cond ((point? obj)
                  (##wr-str we "<")
                  (##wr we (point-x obj))
                  (##wr-str we " ")
                  (##wr we (point-y obj))
                  (##wr-str we ">"))
                 ((rect? obj)
                  (##wr-str we "{")
                  (##wr we (rect-p1 obj))
                  (##wr-str we " ")
                  (##wr we (rect-p2 obj))
                  (##wr-str we "}"))
                 (else
                  (old-wr we obj))))))

;; this prints: {<1 2> <3 4>}

(pretty-print
  (make-rect (make-point 1 2)
             (make-point 3 4)))

Marc




More information about the Gambit-list mailing list