- (define-compound-accessors STRUCT (CTOR ARGUMENT ...) SLOTSPEC ...)syntax
Defines procedures that construct and access instances of a struct or union previously declared with define-struct, define-union, declare-struct or dexclare-union.
CTOR names the constructor taking the given arguments when initializing a structure or union instance. The arguments must match the slot names, omitted slots are uninitialized.
STRUCT may be one of the forms (struct NAME), (union NAME), (typename NAME) or (pointer S) (with S being one of the former). If pointer is given, then the generated constructor will allocate a structure or union instance via malloc(3), otherwise the constructor will create and return a structure instance by value. A plain symbol is interpreted as (typename S).
Each SLOTSPEC should be a list of 2 or 3 elements (SLOTNAME GETTER [SETTER]) where GETTER and SETTER (if given) designate getter and setter procedures to read or write the respective slot value:
(GETTER INSTANCE [INDEX]) => VALUE (SETTER INSTANCE [INDEX] VALUE)
INDEX is required for slots that are declared to have multiple elements.
To release the storage allocated for pointer-struct/union constructors use the free primitive from the (chicken memory) module.
Example:
(import (scheme base) (scheme write) (crunch memory) (chicken memory) (crunch aggregate-types)) (define-struct point (x float) (y float)) ; => struct point {float x, y;} ;; by value (define-compound-accessors (struct point) (make-point x y) (x get-x set-x) (y get-y set-y)) ;; by reference, use "free" to release (define-compound-accessors (pointer (struct point)) (alloc-point x y) (x get-x* set-x*) (y get-y*)) (define (main) (let ((x (make-point 123 4.5)) (y (alloc-point 2 1.23))) (write (vector (get-x x) (get-y x) (get-x* y) (get-y* y))) (newline) (set! x (pointer-ref y)) (write (vector (get-x x) (get-y x) (get-x* y) (get-y* y))) (newline) (free y))) ; writes: ; #f64(123 4.5 2 1.23) ; #f64(2 1.23 2 1.23)