Commit | Line | Data |
---|---|---|
a6fd89a4 MG |
1 | ;;;; srfi-9.scm --- SRFI-9 procedures for Guile |
2 | ;;;; | |
3 | ;;;; Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | ;;;; | |
5 | ;;;; This program is free software; you can redistribute it and/or | |
6 | ;;;; modify it under the terms of the GNU General Public License as | |
7 | ;;;; published by the Free Software Foundation; either version 2, or | |
8 | ;;;; (at your option) any later version. | |
9 | ;;;; | |
10 | ;;;; This program 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 | ;;;; General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU General Public License | |
16 | ;;;; along with this software; see the file COPYING. If not, write to | |
17 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
18 | ;;;; Boston, MA 02111-1307 USA | |
19 | ||
e1633bf3 MG |
20 | ;;; Commentary: |
21 | ||
a6fd89a4 MG |
22 | ;;; This module exports the syntactic form `define-record-type', which |
23 | ;;; is the means for creating record types defined in SRFI-9. | |
24 | ;;; | |
25 | ;;; The syntax of a record type definition is: | |
26 | ;;; | |
27 | ;;; <record type definition> | |
28 | ;;; -> (define-record-type <type name> | |
29 | ;;; (<constructor name> <field tag> ...) | |
30 | ;;; <predicate name> | |
31 | ;;; <field spec> ...) | |
32 | ;;; | |
33 | ;;; <field spec> -> (<field tag> <accessor name>) | |
34 | ;;; -> (<field tag> <accessor name> <modifier name>) | |
35 | ;;; | |
36 | ;;; <field tag> -> <identifier> | |
37 | ;;; <... name> -> <identifier> | |
38 | ;;; | |
39 | ;;; Usage example: | |
40 | ;;; | |
41 | ;;; guile> (use-modules (srfi srfi-9)) | |
42 | ;;; guile> (define-record-type :foo (make-foo x) foo? | |
43 | ;;; (x get-x) (y get-y set-y!)) | |
44 | ;;; guile> (define f (make-foo 1)) | |
45 | ;;; guile> f | |
46 | ;;; #<:foo x: 1 y: #f> | |
47 | ;;; guile> (get-x f) | |
48 | ;;; 1 | |
49 | ;;; guile> (set-y! f 2) | |
50 | ;;; 2 | |
51 | ;;; guile> (get-y f) | |
52 | ;;; 2 | |
53 | ;;; guile> f | |
54 | ;;; #<:foo x: 1 y: 2> | |
55 | ;;; guile> (foo? f) | |
56 | ;;; #t | |
57 | ;;; guile> (foo? 1) | |
58 | ;;; #f | |
59 | ||
e1633bf3 MG |
60 | ;;; Code: |
61 | ||
a6fd89a4 MG |
62 | (define-module (srfi srfi-9)) |
63 | ||
64 | (export-syntax define-record-type) | |
65 | ||
66 | (define-macro (define-record-type type-name constructor/field-tag | |
67 | predicate-name . field-specs) | |
68 | `(begin | |
69 | (define ,type-name | |
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)) | |
75 | ,@(map | |
76 | (lambda (spec) | |
77 | (cond | |
78 | ((= (length spec) 2) | |
79 | `(define ,(cadr spec) | |
80 | (record-accessor ,type-name ',(car spec)))) | |
81 | ((= (length spec) 3) | |
82 | `(begin | |
83 | (define ,(cadr spec) | |
84 | (record-accessor ,type-name ',(car spec))) | |
85 | (define ,(caddr spec) | |
86 | (record-modifier ,type-name ',(car spec))))) | |
87 | (else | |
88 | (error "invalid field spec " spec)))) | |
89 | field-specs))) |