struct-define:   Short-hand accessors for struct fields
struct-define
define-struct-define
8.12

struct-define: Short-hand accessors for struct fields🔗ℹ

Jay McCarthy

 (require struct-define) package: struct-define

struct-define provides a short-hand way of accessing the fields of structs by the name used in the definition of the structure.

syntax

(struct-define some-struct some-instance)

If some-struct is defined with (struct some-struct (f0 ... fN)), then defines f0 (through fN) as macros that expand to (some-struct-f0 some-instance). If a field f0 is mutable, then (set! f0 x) expands to (set-some-struct-f0! some-instance x). (Note: This explanation implies that struct-define makes an assumption about the name of the accessors, but it actually uses whatever the real identifiers embedded in the static struct record are.)

syntax

(define-struct-define the-struct-define the-struct)

Defines the-struct-define such that (the-struct-define the-instance) expands to (struct-define the-struct the-instance).

Examples:
> (require struct-define)
> (struct pie (flavor [temp #:mutable]))
> (define p1 (pie 'cherry 'warm))
> (list (pie-flavor p1) (pie-temp p1))

'(cherry warm)

> (let ()
    (struct-define pie p1)
    (list flavor temp))

'(cherry warm)

> (define-struct-define pie-define pie)
> (let ()
    (pie-define p1)
    (set! temp 'cold)
    (list flavor temp))

'(cherry cold)

> (list (pie-flavor p1) (pie-temp p1))

'(cherry cold)