structures
A structure type is a record data type composing a number of slots. A
structure, an instance of a structure type, is a first-class value
that contains a value for each field of the structure type.
Structures can be created with the define-struct high
level syntax. However, STKLOS also offers some low-level functions
to build and access the internals or a structure.
| define-struct <name> <slot> ...
|
STKLOS Syntax |
Defines a structure type whose name is <name>. Once a structure type is
defined, the following symbols are bound:
<name> denotes the structure type.
make-<name> is a procedure which takes 0 to n parameters (if there
are n slots defined). Each parameter is assigned to the corresponding
field (in the definition order).
<name>? is a predicate which returns #t when applied to an
instance of the <name> structure type and #f otherwise.
<name>-<slot> (one for each defined <slot>) to read the
content of an instance of the <name> structure type. Writting the
content of a slot can be done using a generalized set!.
(define-struct point x y)
(define p (make-point 1 2))
(point? p) => #t
(point? 100) => #f
(point-x p) => 1
(point-y p) => 2
(set! (point-x p) 10)
(point-x p) => 10
|
| make-struct-type name parent slots
|
STKLOS Procedure |
This form which is more general than define-struct permits to define a
new structure type whose name is name. Parent is the structure
type from which is the new structure type is a subtype (or #f is the
new structure-type has no super type). Slots is the list of the slot
names which cconstitute the structure tpe.
When a structure type is s subtype of a previous type, its slots are added
to the ones of the super type.
|
| struct-type? obj
|
STKLOS Procedure |
Returns #t if obj is a structure type, otherwise return #f.
(let ((type (make-struct-type 'point #f '(x y))))
(struct-type? type)) => #t
|
| struct-type-slots structype
|
STKLOS Procedure |
Returns the slots of the structure type structype as a list.
(define point (make-struct-type 'point #f '(x y)))
(define circle (make-struct-type 'circle point '(r)))
(struct-type-slots point) => (x y)
(struct-type-slots circle) => (x y r)
|
| struct-type-slots structype
|
STKLOS Procedure |
Returns the super type of the structure type structype, if it exists
or #f otherwise.
|
| struct-type-name structype
|
STKLOS Procedure |
Returns the name associated to the structure type structype.
|
| struct-type-change-writer! structype proc
|
STKLOS Procedure |
Change the default writer associated to structures of type structype to
to the proc procedure. The proc procedure must accept 2 arguments
(the structure to write and the port wher the structure must be written
in that order). The value returned by struct-type-change-writer! is the
old writer associated to structype. To restore the standard wtructure
writer for structype, use the special value #f.
(define point (make-struct-type 'point #f '(x y)))
(struct-type-change-writer!
point
(lambda (s port)
(let ((type (struct-type s)))
(format port "#[~A" (struct-type-name type))
;; display the slots and their value
(for-each (lambda (x)
(format port " ~A=~S" x (struct-ref s x)))
(struct-type-slots type))
(format port "]"))))
(display (make-struct point 1 2)) => writes #[point x=1 y=2]
|
| make-struct structype expr ...
|
STKLOS Procedure |
Returns a newly allocated instance of the structure type structype,
whose slots are initialized to expr ... If fewer expr than the number of
instances are given to make-struct, the remaining slots are inialized with
the special void object.
|
| struct? obj
|
STKLOS Procedure |
Returns #t if obj is a structure, otherwise return #f#.
(let* ((type (make-struct-type 'point #f '(x y)))
(inst (make-struct type 1 2)))
(struct? inst)) => #t
|
| struct-type s
|
STKLOS Procedure |
Returns the structure type of the s structure
|
| struct-ref s slot-name
|
STKLOS Procedure |
Returns the value associated to slot slot-name of the s structure.
(define point (make-struct-type 'point #f '(x y)))
(define circle (make-struct-type 'circle point '(r)))
(define p (make-struct point 1 2))
(define c (make-struct circle 10 20 30))
(struct-ref p 'y) => 2
(struct-ref c 'r) => 30
|
| struct-set! s slot-name value
|
STKLOS Procedure |
Stores value in the to slot slot-name of the s structure. The value
returned by struct-set! is void.
(define point (make-struct-type 'point #f '(x y)))
(define p (make-struct point 1 2))
(struct-ref p 'x) => 1
(struct-set! p 'x 0)
(struct-ref p 'x) => 0
|
| struct-is-a? s structype
|
STKLOS Procedure |
Return a boolean that indicates if the structure s is a of type structype.
Note that if s is an instance of a subtype of S, it is considered
also as an instance of type S
returned by struct-set! is void.
(define point (make-struct-type 'point #f '(x y)))
(define circle (make-struct-type 'circle point '(r)))
(define p (make-struct point 1 2))
(define c (make-struct circle 10 20 30))
(struct-is-a? p point) => #t
(struct-is-a? c point) => #t
(struct-is-a? p circle) => #f
(struct-is-a? c circle) => #t
|
| struct-list s
|
STKLOS Procedure |
Returns the content of structure s as an A-list whose keys are the
slots of the structure type of s.
(define point (make-struct-type 'point #f '(x y)))
(define p (make-struct point 1 2))
(struct->list p) => ((x . 1) (y . 2))
|