Node:Record types, Next:Dynamic records, Previous:Objects Classes and Modules, Up:Objects Classes and Modules
The define-record-type
form can be used for creating new data
types, called record types. A predicate, constructor, and field
accessors and modifiers are defined for each record type.
The define-record-type
feature is specified
by SRFI-9,
which is implemented by many modern Scheme implementations.
define-record-type type-name (constructor-name field-tag ...) predicate-name (field-tag accessor-name [modifier-name]) ... | Syntax |
The form An instance of
Set!ing the value of any of these identifiers has no effect on the behavior of any of their original values. |
Here is an example of how you can define a record type named pare
with two fields x
and y
:
(define-record-type pare (kons x y) pare? (x kar set-kar!) (y kdr))
The above defines kons
to be a constructor,
kar
and kdr
to be accessors,
set-kar!
to be a modifier,
and pare?
to be a predicate for pare
s.
(pare? (kons 1 2)) --> #t (pare? (cons 1 2)) --> #f (kar (kons 1 2)) --> 1 (kdr (kons 1 2)) --> 2 (let ((k (kons 1 2))) (set-kar! k 3) (kar k)) --> 3
The Kawa compiler creates a new Java class with a name derived from
the type-name. If the type-name is valid Java class name,
that becomes the name of the Java class. If the type-name has
the form <
name>
(for example <pare>
), then name
is used, if possible, for the Java class name. Otherwise, the name
of the Java class is derived by "mangling" the type-name.
In any case, the package is the same as that of the surrounding module.
Kawa generates efficient code for the predicate-name, accessor-name, and modifier-name functions. The constructor-name currently compiles into code using run-time reflection, but hopefully that will get fixed.