summaryrefslogtreecommitdiffstats
path: root/recobj.scm
blob: a634423aa14e9a92aa54edb46018ae29fccbf8c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;;; "recobj.scm" Records implemented as objects.
;;; Author: Wade Humeniuk <humeniuw@cadvision.com>
;;;
;;; This code is in the public domain.

(require 'object)
(require 'common-list-functions)
(define field:position position)
;@
(define record-type-name (make-generic-method))
(define record-accessor (make-generic-method))
(define record-modifier (make-generic-method))
(define record? (make-generic-predicate))
(define record-constructor (make-generic-method))
;@
(define (make-record-type type-name field-names)
  (define self (make-object))

  (make-method! self record-type-name
		(lambda (self)
		  type-name))
  (make-method! self record-accessor
		(lambda (self field-name)
		  (let ((index (field:position field-name field-names)))
		    (if (not index)
			(slib:error "record-accessor: invalid field-name argument."
				    field-name))
		    (lambda (obj)
		      (record-accessor obj index)))))

  (make-method! self record-modifier
		(lambda (self field-name)
		  (let ((index (field:position field-name field-names)))
		    (if (not index)
			(slib:error "record-accessor: invalid field-name argument."
				    field-name))
		    (lambda (obj newval)
		      (record-modifier obj index newval)))))

  (make-method! self record? (lambda (self) #t))

  (make-method! self record-constructor
		(lambda (class . field-values)
		  (let ((values (apply vector field-values)))
		    (define self (make-object))
		    (make-method! self record-accessor
				  (lambda (self index)
				    (vector-ref values index)))
		    (make-method! self record-modifier
				  (lambda (self index newval)
				    (vector-set! values index newval)))
		    (make-method! self record-type-name
				  (lambda (self) (record-type-name class)))
		    self)))
  self)

(provide 'record-object)
(provide 'record)