53 lines
19 KiB
HTML
53 lines
19 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>5 Structures</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="icons.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2 </td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3 </td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4 </td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5 </td><td><a href="structures.html" class="tocviewselflink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6 </td><td><a href="mzlib_class.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7 </td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11 </td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12 </td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13 </td><td><a href="input-and-output.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14 </td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15 </td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16 </td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17 </td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><
|
||
|
<span style="font-style: italic">fields</span>. A <a name="(tech._structure)"></a><span style="font-style: italic">structure</span>, an instance of a structure
|
||
|
type, is a first-class value that contains a value for each field of
|
||
|
the structure type. A structure instance is created with a
|
||
|
type-specific <a href="define-struct.html#%28tech._constructor%29" class="techoutside" data-pltdoc="x"><span class="techinside">constructor</span></a> procedure, and its field values are
|
||
|
accessed and changed with type-specific <a href="define-struct.html#%28tech._accessor%29" class="techoutside" data-pltdoc="x"><span class="techinside">accessor</span></a> and
|
||
|
<a href="define-struct.html#%28tech._mutator%29" class="techoutside" data-pltdoc="x"><span class="techinside">mutator</span></a> procedures. In addition, each structure type has a
|
||
|
<a href="define-struct.html#%28tech._predicate%29" class="techoutside" data-pltdoc="x"><span class="techinside">predicate</span></a> procedure that answers <span class="RktVal">#t</span> for instances of
|
||
|
the structure type and <span class="RktVal">#f</span> for any other value.</p><p>A structure type’s fields are essentially unnamed, though names are
|
||
|
supported for error-reporting purposes. The constructor procedure
|
||
|
takes one value for each field of the structure type, except that some
|
||
|
of the fields of a structure type can be <a name="(tech._automatic._field)"></a><span style="font-style: italic">automatic fields</span>;
|
||
|
the <a href="structures.html#%28tech._automatic._field%29" class="techoutside" data-pltdoc="x"><span class="techinside">automatic fields</span></a> are initialized to a constant that is
|
||
|
associated with the structure type, and the corresponding arguments
|
||
|
are omitted from the constructor procedure. All automatic fields in a
|
||
|
structure type follow the non-automatic fields.</p><p>A structure type can be created as a <a name="(tech._structure._subtype)"></a><span style="font-style: italic">structure subtype</span> of
|
||
|
an existing base structure type. An instance of a structure subtype
|
||
|
can always be used as an instance of the base structure type, but the
|
||
|
subtype gets its own predicate procedure, and it may have its own
|
||
|
fields in addition to the fields of the base type.</p><p>A structure subtype “inherits” the fields of its base type. If the
|
||
|
base type has <span style="font-style: italic">m</span> fields, and if <span style="font-style: italic">n</span> fields are specified
|
||
|
for the new structure subtype, then the resulting structure type has
|
||
|
<span style="font-style: italic">m+n</span> fields. The value for automatic fields can be different in
|
||
|
a subtype than in its base type.</p><p>If <span style="font-style: italic">m</span>′<span style="font-style: italic"></span> of the original <span style="font-style: italic">m</span> fields are non-automatic (where
|
||
|
<span style="font-style: italic">m</span>′<span style="font-style: italic"><m</span>), and <span style="font-style: italic">n</span>′<span style="font-style: italic"></span> of the new fields are non-automatic (where
|
||
|
<span style="font-style: italic">n</span>′<span style="font-style: italic"><n</span>), then <span style="font-style: italic">m</span>′<span style="font-style: italic">+n</span>′<span style="font-style: italic"></span> field values must be provided to the
|
||
|
subtype’s constructor procedure. Values for the first <span style="font-style: italic">m</span> fields
|
||
|
of a subtype instance are accessed with selector procedures for the
|
||
|
original base type (or its supertypes), and the last <span style="font-style: italic">n</span> are
|
||
|
accessed with subtype-specific selectors. Subtype-specific
|
||
|
<a href="define-struct.html#%28tech._accessor%29" class="techoutside" data-pltdoc="x"><span class="techinside">accessors</span></a> and <a href="define-struct.html#%28tech._mutator%29" class="techoutside" data-pltdoc="x"><span class="techinside">mutators</span></a> for the first <span style="font-style: italic">m</span> fields do
|
||
|
not exist.</p><p>The <span class="RktSym"><a href="define-struct.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct%29%29" class="RktStxLink" data-pltdoc="x">struct</a></span> form and <span class="RktSym"><a href="creatingmorestructs.html#%28def._%28%28quote._~23~25kernel%29._make-struct-type%29%29" class="RktValLink" data-pltdoc="x">make-struct-type</a></span>
|
||
|
procedure typically create a new structure type, but they can also
|
||
|
access <a name="(tech._prefab)"></a><span style="font-style: italic">prefab</span> (i.e., previously fabricated) structure types
|
||
|
that are globally shared, and whose instances can be parsed and
|
||
|
written by the default reader (see <a href="reader.html" data-pltdoc="x">The Reader</a>) and printer (see
|
||
|
<a href="printing.html" data-pltdoc="x">The Printer</a>). Prefab structure types can inherit only from
|
||
|
other prefab structure types, and they cannot have guards (see
|
||
|
<a href="creatingmorestructs.html" data-pltdoc="x">Creating Structure Types</a>) or properties (see
|
||
|
<a href="structprops.html" data-pltdoc="x">Structure Type Properties</a>). Exactly one prefab structure type exists for
|
||
|
each combination of name, supertype, field count, automatic field
|
||
|
count, automatic field value (when there is at least one automatic
|
||
|
field), and field mutability.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="magnify.png" alt="+" width="24" height="24"/></span><a href="serialization.html" data-pltdoc="x">Serialization</a> also provides information on reading and writing structures.</p></blockquote></blockquote></blockquote><p><a name="(idx._(gentag._193._(lib._scribblings/reference/reference..scrbl)))"></a>Two structure values are
|
||
|
<span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span> if and only if they are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29" class="RktValLink" data-pltdoc="x">eq?</a></span>. Two structure
|
||
|
values are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span> if they are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29" class="RktValLink" data-pltdoc="x">eq?</a></span>. By default, two
|
||
|
structure values are also <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span> if they are instances of the
|
||
|
same structure type, no fields are opaque, and the results of applying
|
||
|
<span class="RktSym"><a href="structutils.html#%28def._%28%28quote._~23~25kernel%29._struct-~3evector%29%29" class="RktValLink" data-pltdoc="x">struct->vector</a></span> to the structs are
|
||
|
<span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span>. (Consequently, <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span> testing for
|
||
|
structures may depend on the current inspector.) A structure type can
|
||
|
override the default <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span> definition through the
|
||
|
<span class="RktSym"><a href="Equality.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal%2Bhash%29%29" class="RktValLink" data-pltdoc="x">gen:equal+hash</a></span> or <span class="RktSym"><a href="Equality.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._gen~3aequal-mode%2Bhash%29%29" class="RktValLink" data-pltdoc="x">gen:equal-mode+hash</a></span> <a href="struct-generics.html#%28tech._generic._interface%29" class="techoutside" data-pltdoc="x"><span class="techinside">generic interface</span></a>.</p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="hspace"> </span><a href="define-struct.html" class="toclink" data-pltdoc="x">5.1<span class="hspace"> </span>Defining Structure Types: <span class="RktSym"><span class="RktStxLink">struct</span></span></a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="creatingmorestructs.html" class="toclink" data-pltdoc="x">5.2<span class="hspace"> </span>Creating Structure Types</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="structprops.html" class="toclink" data-pltdoc="x">5.3<span class="hspace"> </span>Structure Type Properties</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="struct-generics.html" class="toclink" data-pltdoc="x">5.4<span class="hspace"> </span>Generic Interfaces</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="struct-copy.html" class="toclink" data-pltdoc="x">5.5<span class="hspace"> </span>Copying and Updating Structures</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="structutils.html" class="toclink" data-pltdoc="x">5.6<span class="hspace"> </span>Structure Utilities</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="structutils.html#%28part._.Additional_.Structure_.Utilities%29" class="toclink" data-pltdoc="x">5.6.1<span class="hspace"> </span>Additional Structure Utilities</a></p></td></tr><tr><td><p><span class="hspace"> </span><a href="structinfo.html" class="toclink" data-pltdoc="x">5.7<span class="hspace"> </span>Structure Type Transformer Binding</a></p></td></tr></table><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="undefined.html" title="backward to "4.21 Undefined"" data-pltdoc="x">← prev</a> <a href="index.html" title="up to "The Racket Reference"" data-pltdoc="x">up</a> <a href="define-struct.html" title="forward to "5.1 Defining Structure Types: struct"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|