Debian Codegraph @

lib/util/record.scm

raw
1 ;;;
2 ;;; util/record.scm - guile and SCM compatible make-record-type
3 ;;;
4 ;;; Copyright(C) 2002 by Alex Shinn ([email protected])
5 ;;;
6 ;;; Permission to use, copy, modify, distribute this software and
7 ;;; accompanying documentation for any purpose is hereby granted,
8 ;;; provided that existing copyright notices are retained in all
9 ;;; copies and that this notice is included verbatim in all
10 ;;; distributions.
11 ;;; This software is provided as is, without express or implied
12 ;;; warranty. In no circumstances the author(s) shall be liable
13 ;;; for any damages arising out of the use of this software.
14 ;;;
15
16 ;;; This code is originally written by Alex Shinn as a patch to
17 ;;; SRFI-9. Shiro Kawai modified it slightly to be an independent
18 ;;; module.
19
20 (define-module util.record
21 (extend srfi.9)
22 (use scheme.list)
23 (export record? make-record-type
24 record-constructor record-predicate record-accessor
25 record-modifier record-type-descriptor record-type-name
26 record-type-fields))
27 (select-module util.record)
28
29 ;;
30 ;; Guile and SCM compatible make-record-type
31 ;;
32
33 (define (record? obj) (is-a? obj <record>))
34
35 (define (make-record-type name fields)
36 (unless (every symbol? fields)
37 (error "make-record-type: fields must be a list of symbols" fields))
38 (unless (= (length fields) (length (delete-duplicates fields)))
39 (error "make-record-type: fields must not contain duplicates" fields))
40 (make <class>
41 :supers (list <record>)
42 :slots (map list fields)
43 :name (string->symbol (x->string name))))
44
45 (define (record-constructor record-class :optional (fields (record-type-fields record-class)))
46 (lambda args
47 (let ((rec (make record-class)))
48 (for-each (lambda (f v) (slot-set! rec f v)) fields args)
49 rec)))
50
51 (define (record-predicate record-class)
52 (lambda (obj) (is-a? obj record-class)))
53
54 (define (record-accessor record-class field)
55 (lambda (rec) (slot-ref rec field)))
56
57 (define (record-modifier record-class field)
58 (lambda (rec value) (slot-set! rec field value)))
59
60 (define record-type-descriptor class-of)
61
62 (define (record-type-name record-class)
63 (slot-ref record-class 'name))
64
65 (define (record-type-fields record-class)
66 (map (^x (if (pair? x) (car x) x))
67 (compute-slots record-class)))