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

Last change on this file since 12880 was 12880, checked in by sjamaan, 12 years ago

Temporary hack to fix example sections in wiki eggdoc pages

File size: 3.4 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=== Download
20
21[[http://www.call-with-current-continuation.org/eggs/defstruct.egg|defstruct.egg]]
22
23=== Documentation
24
25<macro>(defstruct NAME SLOT ...)</macro>
26
27Defines a record type with the name {{NAME}}. {{SLOT}} may be a symbol
28or a list of the form {{(NAME INIT)}} where {{INIT}} is the default
29value of the slot (the {{INIT}} expression will only be evaluated when
30no value is given in the constructor procedure).  The {{defstruct}}
31macro expands into predicate and accessor functions (just like the
32native {{define-record}}):
33
34<examples><example>
35<init>
36(require-extension defstruct)
37</init>
38<expr>(defstruct point x y)</expr>
39<result>
40(begin
41  (define (point? x) ...)          ; is x a point?
42  (define (point-x p) ...)         ; return x slot of point p
43  (define (point-x-set! p n) ...)  ; change x slot of point p to n
44  ...)
45</result>
46</example></examples>
47
48Additionally, a constructor procedure {{make-STRUCTNAME}} is defined,
49which accepts initialization values for all slots specified as keyword
50arguments:
51
52<examples><example>
53<expr>
54(define p1 (make-point x: 99 y: 42))
55p1
56</expr>
57<result>; a point with x = 99 and y = 42</result>
58</example></examples>
59
60On Chicken 4 and higher, there are also two procedures {{update-STRUCTNAME}}
61and {{set-STRUCTNAME!}} defined for functionally and destructively updating
62selected values in an existing record:
63
64<examples><example>
65<expr>
66(define p2 (update-point p1 x: 100))
67p2
68</expr>
69<result>; a point with x = 100 and y = 42</result>
70<expr>
71(= p1-x 99)
72</expr>
73<result>#t</result>
74<expr>
75(set-point! p1 x: 100)
76(= p1-x 100)
77</expr>
78<result>#t</result>
79</example></examples>
80
81=== License
82
83  Copyright (c) 2005, Dorai Sitaram
84  Copyright (c) 2005, Felix Winkelmann (Chicken port)
85  Copyright (c) 2008, Peter Bex (Hygienic Chicken port + extensions)
86  All rights reserved.
87 
88  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
89  conditions are met:
90 
91    Redistributions of source code must retain the above copyright notice, this list of conditions and the following
92      disclaimer.
93    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
94      disclaimer in the documentation and/or other materials provided with the distribution.
95    Neither the name of the author nor the names of its contributors may be used to endorse or promote
96      products derived from this software without specific prior written permission.
97 
98  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
99  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
100  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
101  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
102  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
103  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
104  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
105  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
106  POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.