* doc srfi-9 records
@ 2004-02-15 23:57 Kevin Ryde
0 siblings, 0 replies; only message in thread
From: Kevin Ryde @ 2004-02-15 23:57 UTC (permalink / raw)
* srfi-modules.texi (SRFI-9): Revise for detail and clarity. Don't
use ":foo" for example type name, since that depends on the keyword
reading option.
SRFI-9 - define-record-type
===========================
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 (*note Records::). It can be used with,
(use-modules (srfi srfi-9))
- library syntax: define-record-type type
(constructor fieldname ...)
predicate
(fieldname accessor [modifier]) ...
Create a new record type, and make various `define's 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 per `record-type-name'.
CONSTRUCTOR is bound to a function to be called as `(CONSTRUCTOR
fieldval ...)' 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 the `define-record-type' form.
The FIELDNAMEs provide the names for the record fields, as per the
core `record-type-fields' etc, and are referred to in the
subsequent accessor/modifier forms.
PREDICTATE is bound to a function to be called as `(PREDICATE
obj)'. It returns `#t' or `#f' according to whether OBJ is a
record of this type.
Each ACCESSOR is bound to a function to be called `(ACCESSOR
record)' to retrieve the respective field from a RECORD.
Similarly each MODIFIER is bound to a function to be called
`(MODIFIER record 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's set
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
`define's. They can be redefined or `set!' as desired, exported from a
module, etc.
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2004-02-15 23:57 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-15 23:57 doc srfi-9 records Kevin Ryde
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).