source: project/wiki/eggref/5/defstruct @ 35627

Last change on this file since 35627 was 35627, checked in by sjamaan, 3 years ago

Add requirement on srfi-1 to defstruct docs

File size: 4.2 KB
Line 
1[[tags: egg]]
2
3== defstruct
4
5[[toc:]]
6
7=== Description
8
9A more convenient form of {{define-record}}
10
11=== Author
12
13Dorai Sitaram, ported to CHICKEN by [[/users/felix winkelmann|felix winkelmann]], with improvements by [[/users/peter-bex|Peter Bex]]
14
15=== Requirements
16
17This only needs the [[srfi-1]] egg.
18
19=== Documentation
20
21<macro>(defstruct NAME SLOT ...)</macro>
22
23Defines a record type with the name {{NAME}}. {{SLOT}} may be a symbol
24or a list of the form {{(NAME INIT)}} where {{INIT}} is the default
25value of the slot (the {{INIT}} expression will only be evaluated when
26no value is given in the constructor procedure).  The {{defstruct}}
27macro expands into predicate and accessor functions (just like the
28native {{define-record}}):
29
30<enscript highlight="scheme">
31(import defstruct)
32
33(defstruct point x y)
34
35;; Creates code equivalent to the following:
36(begin
37  (define (point? x) ...)          ; is x a point?
38  (define (point-x p) ...)         ; return x slot of point p
39  (define (point-x-set! p n) ...)  ; change x slot of point p to n
40  ...)
41</enscript>
42
43Additionally, a constructor procedure {{make-STRUCTNAME}} is defined,
44which accepts initialization values for all slots specified as keyword
45arguments:
46
47<enscript highlight="scheme">
48(define p1 (make-point x: 99 y: 42))
49 => ; a point with x = 99 and y = 42
50</enscript>
51
52There are also two procedures {{update-STRUCTNAME}} and
53{{set-STRUCTNAME!}} defined for functionally and destructively
54updating selected values in an existing record:
55
56<enscript highlight="scheme">
57(define p2 (update-point p1 x: 100))
58p2
59 => ; a point with x = 100 and y = 42
60
61(= p1-x 99)
62 => #t
63
64(set-point! p1 x: 100)
65(= p1-x 100)
66 => #t
67</enscript>
68
69Finally, there are two conversion procedures for converting to and
70from alists, {{STRUCTNAME->alist}} and {{alist->STRUCTNAME}}:
71
72<enscript highlight="scheme">
73(define p3 (alist->point '((x . 1) (y . 2))))
74p3
75 => ; a point with x = 1 and y = 2
76
77(define p4 (make-point x: 123 y: 456))
78(point->alist p4)
79 => ((x . 123) (y . 456))
80</enscript>
81
82=== Bugs and limitations
83
84This macro is unhygienic which, while resulting in concise syntax, has
85some drawbacks:
86
87* Just like with {{define-record}} you cannot use field names ending with {{-set!}} if you also have a similar field name not ending with {{-set!}}.
88* Just like with {{define-record}} you should avoid defining record types with a name that is a prefix of another record type's name in the same module, because that might cause naming clashes.
89* For the same reason, you should avoid defining record types named {{make}} or {{update}}.
90* You cannot use a field named {{>alist}}.
91
92=== License
93
94  Copyright (c) 2005, Dorai Sitaram
95  Copyright (c) 2005, Felix Winkelmann (Chicken port)
96  Copyright (c) 2008-2018, Peter Bex (Hygienic Chicken port + extensions)
97  All rights reserved.
98 
99  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
100  conditions are met:
101 
102    Redistributions of source code must retain the above copyright notice, this list of conditions and the following
103      disclaimer.
104    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
105      disclaimer in the documentation and/or other materials provided with the distribution.
106    Neither the name of the author nor the names of its contributors may be used to endorse or promote
107      products derived from this software without specific prior written permission.
108 
109  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
110  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
111  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
112  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
113  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
114  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
115  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
116  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
117  POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.