This SRFI is a syntax for defining new record types and creating predicate, constructor, and field getter and setter functions. In Guile this is simply an alternate interface to the core record functionality (see Records). It can be used with,
(use-modules (srfi srfi-9))
Create a new record type, and make variousdefines for using it. This syntax can only occur at the top-level, not nested within some other form.type is bound to the record type, which is as per the return from the core
make-record-type. type also provides the name for the record, as perrecord-type-name.constructor is bound to a function to be called as
(constructorfieldval ...)to create a new record of this type. The arguments are initial values for the fields, one argument for each field, in the order they appear in thedefine-record-typeform.The fieldnames provide the names for the record fields, as per the core
record-type-fieldsetc, and are referred to in the subsequent accessor/modifier forms.predicate is bound to a function to be called as
(predicateobj). It returns#tor#faccording to whether obj is a record of this type.Each accessor is bound to a function to be called
(accessorrecord)to retrieve the respective field from a record. Similarly each modifier is bound to a function to be called(modifierrecord val)to set the respective field in a record.
An example will illustrate typical usage,
(define-record-type employee-type
(make-employee name age salary)
employee?
(name get-employee-name)
(age get-employee-age set-employee-age)
(salary get-employee-salary set-employee-salary))
This creates a new employee data type, with name, age and salary fields. Accessor functions are created for each field, but no modifier function for the name (the intention in this example being that it's established only when an employee object is created). These can all then be used as for example,
employee-type ⇒ #<record-type employee-type>
(define fred (make-employee "Fred" 45 20000.00))
(employee? fred) ⇒ #t
(get-employee-age fred) ⇒ 45
(set-employee-salary fred 25000.00) ;; pay rise
The functions created by define-record-type are ordinary
top-level defines. They can be redefined or set! as
desired, exported from a module, etc.
The SRFI-9 specification explicitly disallows record definitions in a
non-toplevel context, such as inside lambda body or inside a
let block. However, Guile's implementation does not enforce that
restriction.
You may use set-record-type-printer! to customize the default printing
behavior of records. This is a Guile extension and is not part of SRFI-9. It
is located in the (srfi srfi-9 gnu) module.
Where type corresponds to the first argument of
define-record-type, and thunk is a procedure accepting two arguments, the record to print, and an output port.
This example prints the employee's name in brackets, for instance [Fred].
(set-record-type-printer! employee-type
(lambda (record port)
(write-char #\[ port)
(display (get-employee-name record) port)
(write-char #\] port)))