8.18
top
← prev up next →

struct-define: Short-hand accessors for struct fieldsπŸ”— i

Jay McCarthy

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-structsome-instance)

If some-struct is defined with (struct some-struct(f0... fN)), then defines f0 (through fN) as macros that expand to (some-struct-f0some-instance). If a field f0 is mutable, then (set! f0x) expands to (set-some-struct-f0!some-instancex). (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-definethe-struct)

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

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

'(cherry warm)

> (let ()
(struct-define piep1)
(list flavortemp))

'(cherry warm)

> (define-struct-define pie-definepie)
> (let ()
(pie-definep1)
(set! temp'cold)
(list flavortemp))

'(cherry cold)

> (list (pie-flavorp1)(pie-tempp1))

'(cherry cold)

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /