source: project/wiki/eggref/4/defstruct @ 13306

Last change on this file since 13306 was 13306, checked in by felix winkelmann, 12 years ago

added xlib page and removed download links

File size: 3.3 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 [[felix winkelmann]], with improvements by [[peter bex]]
14
15=== Requirements
16
17None
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<examples><example>
31<init>
32(require-extension defstruct)
33</init>
34<expr>(defstruct point x y)</expr>
35<result>
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</result>
42</example></examples>
43
44Additionally, a constructor procedure {{make-STRUCTNAME}} is defined,
45which accepts initialization values for all slots specified as keyword
46arguments:
47
48<examples><example>
49<expr>
50(define p1 (make-point x: 99 y: 42))
51p1
52</expr>
53<result>; a point with x = 99 and y = 42</result>
54</example></examples>
55
56On Chicken 4 and higher, there are also two procedures {{update-STRUCTNAME}}
57and {{set-STRUCTNAME!}} defined for functionally and destructively updating
58selected values in an existing record:
59
60<examples><example>
61<expr>
62(define p2 (update-point p1 x: 100))
63p2
64</expr>
65<result>; a point with x = 100 and y = 42</result>
66<expr>
67(= p1-x 99)
68</expr>
69<result>#t</result>
70<expr>
71(set-point! p1 x: 100)
72(= p1-x 100)
73</expr>
74<result>#t</result>
75</example></examples>
76
77=== License
78
79  Copyright (c) 2005, Dorai Sitaram
80  Copyright (c) 2005, Felix Winkelmann (Chicken port)
81  Copyright (c) 2008, Peter Bex (Hygienic Chicken port + extensions)
82  All rights reserved.
83 
84  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
85  conditions are met:
86 
87    Redistributions of source code must retain the above copyright notice, this list of conditions and the following
88      disclaimer.
89    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
90      disclaimer in the documentation and/or other materials provided with the distribution.
91    Neither the name of the author nor the names of its contributors may be used to endorse or promote
92      products derived from this software without specific prior written permission.
93 
94  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
95  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
96  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
97  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
98  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
99  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
100  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
101  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
102  POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.