| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
srfi-9 - Record types Contains a macro to use record types. A record type is implemented on top of Gauche's object system.
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.
See also util.record - SLIB-compatible record type,
which provides an SCM-compatible
procedural interface of record types.
Creates a record type and binds it to name.
In Gauche, a record type is just a subclass of <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 ] | [ >> ] |
This document was generated by Shiro Kawai on October, 7 2008 using texi2html 1.78.