1 ;;; srfi-9.scm --- define-record-type
3 ;; Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 2.1 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;; This module exports the syntactic form `define-record-type', which
22 ;; is the means for creating record types defined in SRFI-9.
24 ;; The syntax of a record type definition is:
26 ;; <record type definition>
27 ;; -> (define-record-type <type name>
28 ;; (<constructor name> <field tag> ...)
32 ;; <field spec> -> (<field tag> <accessor name>)
33 ;; -> (<field tag> <accessor name> <modifier name>)
35 ;; <field tag> -> <identifier>
36 ;; <... name> -> <identifier>
40 ;; guile> (use-modules (srfi srfi-9))
41 ;; guile> (define-record-type :foo (make-foo x) foo?
42 ;; (x get-x) (y get-y set-y!))
43 ;; guile> (define f (make-foo 1))
48 ;; guile> (set-y! f 2)
61 (define-module (srfi srfi-9)
62 :export-syntax (define-record-type))
64 (cond-expand-provide (current-module) '(srfi-9))
66 (define-macro (define-record-type type-name constructor/field-tag
67 predicate-name . field-specs)
70 (make-record-type ',type-name ',(map car field-specs)))
71 (define ,(car constructor/field-tag)
72 (record-constructor ,type-name ',(cdr constructor/field-tag)))
73 (define ,predicate-name
74 (record-predicate ,type-name))
80 (record-accessor ,type-name ',(car spec))))
84 (record-accessor ,type-name ',(car spec)))
86 (record-modifier ,type-name ',(car spec)))))
88 (error "invalid field spec " spec))))
91 ;;; srfi-9.scm ends here