record-accessor(3scm)
π 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.
π¨ ifkspecifies 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,kmust be a valid field index ofrtd, which must be a record-type descriptor. therecord-mutatorprocedure furthermore requires that the field specified bykbe 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
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms