2.2 Record Types
(require rebellion/type/record) | package: rebellion |
A record type is a kind of data type for composite values that contain an unordered set of named fields. The definition of each record type declares how many fields it has and what their names are. Constructing an instance of a record type requires passing a keyword argument for each field to the type’s constructor. Record types are useful when a fixed number of different pieces of data together represent a single logical thing, and there isn’t an obvious order to those pieces.
(define-record-type opcode (name argument addressing-mode)) (define add-42 (opcode #:name 'ADD #:argument 42 #:addressing-mode 'immediate))
> add-42 (opcode #:addressing-mode 'immediate #:argument 42 #:name 'ADD)
> (opcode-name add-42) 'ADD
syntax
(define-record-type id (field-id ...) option ...)
option = #:omit-root-binding | #:constructor-name constructor-id | #:predicate-name predicate-id | #:pattern-name pattern-id | #:property-maker prop-maker-expr
prop-maker-expr :
(-> uninitialized-record-descriptor? (listof (cons/c struct-type-property? any/c)))
constructor-id, which defaults to constructor:id —
a constructor function that accepts one mandatory keyword argument for each field-id and returns an instance of the created type. predicate-id, which defaults to id? —
a predicate function that returns #t when given instances of the created type and returns #f otherwise. id-field-id for each field-id —
an accessor function that returns the value for field-id when given an instance of the created type. pattern-id, which defaults to pattern:id —
a match expander that accepts one optional keyword and subpattern pair for each field and deconstructs instances of the created type, matching each field with its corresponding subpattern (if a subpattern for that field is given).
Additionally, unless #:omit-root-binding is specified, the original id is bound to pattern-id when used in match patterns and to constructor-id when used as an expression. Use #:omit-root-binding when you want control over what id is bound to, such as when creating a smart constructor.
(define-record-type color (red green blue)) (define yellow (color #:red 0 #:green 255 #:blue 255))
> yellow (color #:blue 255 #:green 255 #:red 0)
> (color? yellow) #t
> (color-red yellow) 0
> (color-green yellow) 255
> (color-blue yellow) 255
> (match yellow [(color #:red r #:blue b) (list r b)]) '(0 255)
procedure
(record-type? v) → boolean?
v : any/c
procedure
(record-type name fields [ #:predicate-name predicate-name #:constructor-name constructor-name #:accessor-name accessor-name]) → record-type? name : symbol? fields : keyset? predicate-name : (or/c symbol? #f) = #f constructor-name : (or/c symbol? #f) = #f accessor-name : (or/c symbol? #f) = #f
procedure
(record-type-name type) → symbol?
type : record-type?
procedure
(record-type-fields type) → keyset?
type : record-type?
procedure
(record-type-predicate-name type) → (or/c symbol? #f)
type : record-type?
procedure
(record-type-constructor-name type) → (or/c symbol? #f)
type : record-type?
procedure
(record-type-accessor-name type) → (or/c symbol? #f)
type : record-type?
procedure
(make-record-implementation type [ #:inspector inspector #:property-maker prop-maker]) → initialized-record-descriptor? type : record-type? inspector : inspector? = (current-inspector)
prop-maker :
(-> uninitialized-record-descriptor? (listof (cons/c struct-type-property? any/c))) = default-record-properties
procedure
(record-descriptor? v) → boolean?
v : any/c
procedure
v : any/c
procedure
v : any/c
procedure
(record-descriptor-type descriptor) → record-type?
descriptor : record-descriptor?
procedure
(record-descriptor-predicate descriptor) → (-> any/c boolean?)
descriptor : record-descriptor?
procedure
(record-descriptor-constructor descriptor) → procedure?
descriptor : record-descriptor?
procedure
(record-descriptor-accessor descriptor)
→ (-> (record-descriptor-predicate descriptor) natural? any/c) descriptor : record-descriptor?
procedure
(make-record-field-accessor descriptor field) → (-> (record-descriptor-predicate descriptor) any/c) descriptor : record-descriptor? field : natural?
procedure
(default-record-properties descriptor)
→ (listof (cons/c struct-type-property? any/c)) descriptor : record-descriptor?
procedure
(default-record-equal+hash descriptor) → equal+hash/c
descriptor : record-descriptor?
procedure
(default-record-custom-write descriptor)
→ custom-write-function/c descriptor : record-descriptor?