record-accessor, record-mutator - record access and mutations
(import (rnrs)) ;R6RS
(import (rnrs records procedural)) ;R6RS
(record-accessor rtd k)
(record-mutator rtd k)
These procedures return accessors and mutators for field k of the record
type rtd.
- Record accessors
- The record-accessor procedure returns a one-argument procedure
whose argument must be a record of the type represented by rtd.
This procedure returns the value of the selected field of that
record.
- Record mutators
- The record-mutator procedure returns a two-argument procedure whose
arguments must be a record r of the type represented by rtd
and an object obj. This procedure stores obj within the
field of r specified by k.
- If k specifies an immutable field, an exception with condition type
&assertion(3scm) is raised. The mutator returns unspecified
values.
- The field index
- The field selected corresponds to the kth element (0-based) of the
fields argument to the invocation of
make-record-type-descriptor(3scm) that created rtd.
- Note that k cannot be used to specify a field of any type
rtd extends.
These procedures return a single value; a procedure.
; Note that the colon prefix has no special meaning
; in these examples. It is just part of the names.
(define :point
(make-record-type-descriptor
'point #f
#f #f #f
'#((mutable x) (mutable y))))
(define :point-cd
(make-record-constructor-descriptor :point #f #f)
(define make-point (record-constructor :point-cd))
(define point? (record-predicate :point))
(define point-x (record-accessor :point 0))
(define point-y (record-accessor :point 1))
(define point-x-set! (record-mutator :point 0))
(define point-y-set! (record-mutator :point 1))
(define p1 (make-point 1 2))
(point? p1) => #t
(point-x p1) => 1
(point-y p1) => 2
(point-x-set! p1 5) => unspecified
(point-x p1) => 5
(define :point2
(make-record-type-descriptor
'point2 :point
#f #f #f '#((mutable x) (mutable y))))
(define make-point2
(record-constructor
(make-record-constructor-descriptor :point2
#f #f)))
(define point2? (record-predicate :point2))
(define point2-xx (record-accessor :point2 0))
(define point2-yy (record-accessor :point2 1))
(define p2 (make-point2 1 2 3 4))
(point? p2) => #t
(point-x p2) => 1
(point-y p2) => 2
(point2-xx p2) => 3
(point2-yy p2) => 4
(define :point-cd/abs
(make-record-constructor-descriptor
:point #f
(lambda (new)
(lambda (x y)
(new (abs x) (abs y))))))
(define make-point/abs
(record-constructor :point-cd/abs))
(point-x (make-point/abs -1 -2))
=> 1
(point-y (make-point/abs -1 -2))
=> 2
(define :cpoint
(make-record-type-descriptor
'cpoint :point
#f #f #f
'#((mutable rgb))))
(define make-cpoint
(record-constructor
(make-record-constructor-descriptor
:cpoint :point-cd
(lambda (p)
(lambda (x y c)
((p x y) (color->rgb c)))))))
(define make-cpoint/abs
(record-constructor
(make-record-constructor-descriptor
:cpoint :point-cd/abs
(lambda (p)
(lambda (x y c)
((p x y) (color->rgb c)))))))
(define cpoint-rgb
(record-accessor :cpoint 0))
(define (color->rgb c)
(cons 'rgb c))
(cpoint-rgb (make-cpoint -1 -3 'red))
=> (rgb . red)
(point-x (make-cpoint -1 -3 'red))
=> -1
(point-x (make-cpoint/abs -1 -3 'red))
=> 1
These procedures are used together with the procedural record layer. It is more
common for applications to use the syntactic layer, see
define-record-type(3scm).
These procedures are unique to the R6RS record system. Equivalent procedures can
be found in SRFI-237.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
- The wrong number of arguments was passed or an argument was outside its
domain. In particular, k must be a valid field index of rtd,
which must be a record-type descriptor. The record-mutator
procedure furthermore requires that the field specified by k be
mutable.
These procedures first appeared in R6RS as part of the new record system.