diff options
-rw-r--r-- | doc/ref/srfi-modules.texi | 29 | ||||
-rw-r--r-- | module/srfi/srfi-9/gnu.scm | 29 |
2 files changed, 58 insertions, 0 deletions
diff --git a/doc/ref/srfi-modules.texi b/doc/ref/srfi-modules.texi index 0d192fa44..b3f99460b 100644 --- a/doc/ref/srfi-modules.texi +++ b/doc/ref/srfi-modules.texi @@ -1922,6 +1922,35 @@ The functions created by @code{define-record-type} are ordinary top-level @code{define}s. They can be redefined or @code{set!} as desired, exported from a module, etc. +@menu +* SRFI-9 Custom printers:: Customizing print behavior. +@end menu + +@node SRFI-9 Custom printers +@subsubsection Custom printers +@cindex record printer + +You may use @code{set-record-printer!} to customize the default printing +behavior of records. This is a GUILE extension and is not part of SRFI-9. It is +located in the @nicode{(srfi srfi-9 gnu)} module. + +@deffn {library syntax} set-record-printer! name thunk +Where @var{type} corresponds to the first argument of @code{define-record-type}, +and @var{thunk} is a procedure accepting two arguments, the record to print, and +an output port. + +@end deffn + +@noindent +This example prints the employee's name in brackets, for instance ``@code{[Fred]}''. + +@example +(set-record-printer! employee-type + (lambda (record port) + (write-char #\[ port) + (display (get-employee-name record) port) + (write-char #\] port))) +@end example @node SRFI-10 @subsection SRFI-10 - Hash-Comma Reader Extension diff --git a/module/srfi/srfi-9/gnu.scm b/module/srfi/srfi-9/gnu.scm new file mode 100644 index 000000000..3a37471b1 --- /dev/null +++ b/module/srfi/srfi-9/gnu.scm @@ -0,0 +1,29 @@ +;;; Extensions to SRFI-9 + +;; Copyright (C) 2010 Free Software Foundation, Inc. +;; +;; This library is free software; you can redistribute it and/or +;; modify it under the terms of the GNU Lesser General Public +;; License as published by the Free Software Foundation; either +;; version 3 of the License, or (at your option) any later version. +;; +;; This library is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; Lesser General Public License for more details. +;; +;; You should have received a copy of the GNU Lesser General Public +;; License along with this library; if not, write to the Free Software +;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +;;; Commentary: + +;; Extensions to SRFI-9. Fully documented in the Guile Reference Manual. + +;;; Code: + +(define-module (srfi srfi-9 gnu) + #:export (set-record-printer!)) + +(define (set-record-printer! type thunk) + (struct-set! type vtable-index-printer thunk)) |