[gambit-list] overload pp for define-types
Marc Feeley
feeley at iro.umontreal.ca
Sun Jun 28 10:15:02 EDT 2009
On 27-Jun-09, at 10:32 PM, lowly coder wrote:
> Is there a way to overload pp for define-types? (maybe some
> undocumented arg in (define-type ...) ?) I would like to change how
> they're printed. Also, can I override existing ones, like f32vector?
> When I have something that's 1000 f32's, I'd prefer an output of
> <#f32vector ...> rather than all 1000 elems.
>
> Thanks!
>
> (If not, I'll just define a separate command like pp2 and use that
> instead, but overloading pp somehow seems more elegant).
This can be done by redefining the ##wr variable which is normally set
to ##default-wr. The first parameter of the procedure is the "write
environment", which contains the port to write to, and the second
parameter is the object to write.
Here's a quick example:
(define-type person name gender)
(set! ##wr
(lambda (we obj)
(if (person? obj)
(begin
(##wr-str we "{PERSON:")
(##wr-str we " name=") (##wr we (person-name obj))
(##wr-str we " gender=") (##wr we (person-gender obj))
(##wr-str we "}"))
(##default-wr we obj)))) ;; otherwise use default writer
(pp (make-person "marc" 'male)) ;; prints: {PERSON: name="marc"
gender=male}
If you want to "pretty-print" the objects, follow the model of the
procedure ##wr-structure defined in lib/_io.scm .
Marc
More information about the Gambit-list
mailing list