record-accessor(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… record-accessor(3scm) πŸ’…

okay so this page is from the Scheme Programmer’s Manual, bestie, and it’s all about record-accessor and record-mutator β€” record access and mutations, no cap. let’s get into it ✨

πŸŽ€ NAME πŸŽ€

record-accessor, record-mutator β€” record access and mutations. literally the duo that hands you getters and setters for record fields πŸ’–

πŸ“š LIBRARY πŸ“š

(import (rnrs))                     ;R6RS
(import (rnrs records procedural))  ;R6RS

🌸 SYNOPSIS 🌸

(record-accessor rtd k)
(record-mutator rtd k)

✨ DESCRIPTION ✨

okay so these procedures return accessors and mutators for field k of the record type rtd. iconic behavior, honestly.

πŸ’… Record accessors

record-accessor returns a one-argument procedure. that procedure’s argument must be a record of the type represented by rtd. it returns the value of the selected field of that record. slay.

🫢 Record mutators

record-mutator returns a two-argument procedure. those 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(7scm) is raised. the mutator returns unspecified values. periodt.

πŸ¦‹ 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. iykyk.

πŸ‘‘ RETURN VALUES πŸ‘‘

these procedures return a single value; a procedure. one value, and it’s a procedure. that’s the whole vibe.

🌈 EXAMPLES 🌈

bestie this example block is sacred β€” copied exactly, character for character:

; 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

πŸ’œ APPLICATION USAGE πŸ’œ

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(7scm). like, the syntactic layer is lowkey the main character for most apps.

🧚 COMPATIBILITY 🧚

these procedures are unique to the R6RS record system. equivalent procedures can be found in SRFI-237. cute little R6RS exclusive, bestie.

πŸ“ ERRORS πŸ“

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. so yeah, check your args, bestie 😭

πŸ”₯ SEE ALSO πŸ”₯

⭐ STANDARDS ⭐

R6RS. periodt.

πŸ¦‹ HISTORY πŸ¦‹

these procedures first appeared in R6RS as part of the new record system. vintage R6RS behavior, honestly.

πŸ’– AUTHORS πŸ’–

this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more information can be found at https://github.com/schemedoc/manpages/. 🫢


πŸ’… signing off from the scheme-manpages source with the 2023-08-04 date stamp on record-accessor(3scm) β€” slay and goodbye, bestie βœ¨πŸŽ€

πŸ“¦ where this page comes from