Next: , Previous: , Up: iklib records   [Index]


6.19.5 Custom record printers

Vicare’s built–in Scheme objects writer can print records just fine, handling cyclic references and shared objects:

(import (vicare))
(print-graph #t)
(set-port-buffer-mode! (current-output-port)
                       (buffer-mode none))

(define-record-type duo
  (fields one two))

;; simple record
(display (make-duo 1 2))
-| #[record duo one=1 two=2]

;; record with shared object
(let* ((A (make-duo 1 2))
       (B (make-duo A A)))
  (display B))
-| #[record duo one=#0=#[record duo one=1 two=2] two=#0#]

;; record with cyclic reference to itself
(let ((A (make-duo 1 (void))))
  (duo-two-set! A A)
  (display A))
-| #0=#[record duo one=1 two=#0#]

The Scheme objects writer is able to differentiate between display, write and pretty-print printing. When printing records, the built–in writer makes no difference between printing with display and write; it does it differently when printing with pretty-print:

(import (vicare))
(print-graph #t)
(set-port-buffer-mode! (current-output-port)
                       (buffer-mode none))

(define-record-type duo
  (fields one two))

;; simple record
(pretty-print (make-duo 1 2))
-| (record duo (one 1) (two 2))

;; record with shared object
(let* ((A (make-duo 1 2))
       (B (make-duo A A)))
  (pretty-print B))
-| (record duo (one #0=(record duo (one 1) (two 2))) (two #0#))

;; record with cyclic reference to itself
(let ((A (make-duo 1 (void))))
  (duo-two-set! A A)
  (pretty-print A))
-| #0=(record duo (one 1) (two #0#))

Custom printers

Sometimes, we need to print a record with a customised representation. For every record type it is possible to set a custom printer function; it accepts 3 arguments: the record to be printed; a textual output port into which to write a string representation of the record in the style of display, write or pretty-print; a sub–printer function to be optionally used to print component objects. The sub–printer function accepts as single argument the object to print; it allows us to interface with the shared objects printer that handles shared and cyclic references.

As example, the following code defines a custom printer, making use of the parameter printer-printing-style to differentiate the style:

(import (vicare))
(print-graph #t)
(set-port-buffer-mode! (current-output-port)
                       (buffer-mode none))

(define-record-type duo
  (fields one two))

(define (duo-printer stru port sub-printer)
  (case (printer-printing-style)
    ((display)
     (display "#{duo " port)
     (sub-printer (duo-one stru))
     (display " " port)
     (sub-printer (duo-two stru))
     (display "}" port))
    ((write)
     (display "(" port)
     ;;By using the sub-printer: we make this sexp shared too.
     (sub-printer '(record-constructor
                      (record-constructor-descriptor duo)))
     (display " " port)
     (sub-printer (duo-one stru))
     (display " " port)
     (sub-printer (duo-two stru))
     (display ")" port))
    ((pretty-print)
     (sub-printer `(record duo
                           #:one ,(duo-one stru)
                           #:two ,(duo-two stru))))))

(record-type-printer-set! (record-type-descriptor duo)
                          duo-printer)

(define O
  (make-duo 1 2))

(display O)
-| #{duo 1 2}"

(write O)
-| ((record-constructor (record-constructor-descriptor duo)) 1 2)

(pretty-print O)
-| (record duo #:one 1 #:two 2)

;; shared object, display
(let* ((A (make-duo 1 2))
       (B (make-duo A A)))
  (display B))
-| #{duo #0=#{duo 1 2} #0#}

;; shared object, write
(let* ((A (make-duo 1 2))
       (B (make-duo A A)))
  (write B))
-| (#0=(record-constructor (record-constructor-descriptor duo)) \
  #1=(#0# 1 2) #1#)
Function: record-type-printer-set! rtd printer

Select the procedure printer as printer for records of type rtd; if printer is #f, instances of type rtd make use of the built–in Scheme objects printer. Return the old printer function or #f if no printer function was set for this rtd.

Function: record-type-printer rtd

Return the function set as custom printer for records of type rtd; return #f if rtd has no custom printer.

Function: record-printer record

Return #f or the function being the custom record printer for the record record.


Next: , Previous: , Up: iklib records   [Index]