| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
srfi-9 - Record types The SRFI-9 record type is not as powerful as the object system, but it will be useful if you want your program to be portable.
<record>.
constructor is bound to a procedure that creates an instance of the record type, which takes as many arguments as init-tag .... Each init-tag corresponds to one of the field name, and the fields of the created record instance is initialized accordingly. Not all of fields need to appear in init-tag; uninitialized fields remain unbound.
predicate is bound to a procedure that takes one argument, and returns #t if the argument is an instance of the defined record type, #f otherwise.
Followings are field specifications. The record has fields field ..., and each field can be accessed by a method accessor. If modifier is given to the field, it is bound to a method that sets the value to the field.
Example:
(define-record-type pare (kons x y) pare? (x kar set-kar!) (y kdr)) => #<class pare> (pare? (kons 2 3)) => #t (pare? (cons 2 3)) => #f (kar (kons 2 3)) => 2 (kdr (kons 2 3)) => 3 (let ((x (kons 2 3))) (set-kar! x -1) (kar x)) => -1 |
Conceptually, the above example is expanded into the following sequence of forms.
(define-class pare (<record>) (x y))
(define (kons x y)
(let ((obj (make pare)))
(slot-set! obj 'x x)
(slot-set! obj 'y y)
obj))
(define (pare? obj) (is-a? obj pare))
(define-method kar ((obj pare))
(slot-ref obj 'x))
(define-method set-kar! ((obj pare) value)
(slot-set! obj 'x value))
(define-method kdr ((obj pare))
(slot-ref obj 'y))
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |