161 lines
11 KiB
HTML
161 lines
11 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<!-- This manual documents Guile version 3.0.10.
|
||
|
|
||
|
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
|
||
|
Inc.
|
||
|
|
||
|
Copyright (C) 2021 Maxime Devos
|
||
|
|
||
|
Copyright (C) 2024 Tomas Volf
|
||
|
|
||
|
|
||
|
Permission is granted to copy, distribute and/or modify this document
|
||
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
||
|
any later version published by the Free Software Foundation; with no
|
||
|
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
|
||
|
copy of the license is included in the section entitled "GNU Free
|
||
|
Documentation License." -->
|
||
|
<title>Structure Basics (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="Structure Basics (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="Structure Basics (Guile Reference Manual)">
|
||
|
<meta name="resource-type" content="document">
|
||
|
<meta name="distribution" content="global">
|
||
|
<meta name="Generator" content=".texi2any-real">
|
||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
|
|
||
|
<link href="index.html" rel="start" title="Top">
|
||
|
<link href="Concept-Index.html" rel="index" title="Concept Index">
|
||
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
||
|
<link href="Structures.html" rel="up" title="Structures">
|
||
|
<link href="Vtable-Contents.html" rel="next" title="Vtable Contents">
|
||
|
<link href="Vtables.html" rel="prev" title="Vtables">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
|
||
|
div.example {margin-left: 3.2em}
|
||
|
span:hover a.copiable-link {visibility: visible}
|
||
|
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
|
||
|
-->
|
||
|
</style>
|
||
|
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en">
|
||
|
<div class="subsubsection-level-extent" id="Structure-Basics">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Vtable-Contents.html" accesskey="n" rel="next">Vtable Contents</a>, Previous: <a href="Vtables.html" accesskey="p" rel="prev">Vtables</a>, Up: <a href="Structures.html" accesskey="u" rel="up">Structures</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
<hr>
|
||
|
<h4 class="subsubsection" id="Structure-Basics-1"><span>6.6.18.2 Structure Basics<a class="copiable-link" href="#Structure-Basics-1"> ¶</a></span></h4>
|
||
|
|
||
|
<p>This section describes the basic procedures for working with structures.
|
||
|
<code class="code">make-struct/no-tail</code> creates a structure, and <code class="code">struct-ref</code>
|
||
|
and <code class="code">struct-set!</code> access its fields.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-make_002dstruct_002fno_002dtail"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">make-struct/no-tail</strong> <var class="def-var-arguments">vtable init …</var><a class="copiable-link" href="#index-make_002dstruct_002fno_002dtail"> ¶</a></span></dt>
|
||
|
<dd><p>Create a new structure, with layout per the given <var class="var">vtable</var>
|
||
|
(see <a class="pxref" href="Vtables.html">Vtables</a>).
|
||
|
</p>
|
||
|
<p>The optional <var class="var">init</var>… arguments are initial values for the
|
||
|
fields of the structure. This is the only way to
|
||
|
put values in read-only fields. If there are fewer <var class="var">init</var>
|
||
|
arguments than fields then the defaults are <code class="code">#f</code> for a Scheme
|
||
|
field (type <code class="code">p</code>) or 0 for an unboxed field (type <code class="code">u</code>).
|
||
|
</p>
|
||
|
<p>The name is a bit strange, we admit. The reason for it is that Guile
|
||
|
used to have a <code class="code">make-struct</code> that took an additional argument;
|
||
|
while we deprecate that old interface, <code class="code">make-struct/no-tail</code> is the
|
||
|
new name for this functionality.
|
||
|
</p>
|
||
|
<p>For example,
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(define v (make-vtable "pwpwpw"))
|
||
|
(define s (make-struct/no-tail v 123 "abc" 456))
|
||
|
(struct-ref s 0) ⇒ 123
|
||
|
(struct-ref s 1) ⇒ "abc"
|
||
|
</pre></div>
|
||
|
</dd></dl>
|
||
|
|
||
|
<dl class="first-deftypefn">
|
||
|
<dt class="deftypefn" id="index-scm_005fmake_005fstruct"><span class="category-def">C Function: </span><span><code class="def-type">SCM</code> <strong class="def-name">scm_make_struct</strong> <code class="def-code-arguments">(SCM vtable, SCM tail_size, SCM init_list)</code><a class="copiable-link" href="#index-scm_005fmake_005fstruct"> ¶</a></span></dt>
|
||
|
<dt class="deftypefnx def-cmd-deftypefn" id="index-scm_005fc_005fmake_005fstruct"><span class="category-def">C Function: </span><span><code class="def-type">SCM</code> <strong class="def-name">scm_c_make_struct</strong> <code class="def-code-arguments">(SCM vtable, SCM tail_size, SCM init, ...)</code><a class="copiable-link" href="#index-scm_005fc_005fmake_005fstruct"> ¶</a></span></dt>
|
||
|
<dt class="deftypefnx def-cmd-deftypefn" id="index-scm_005fc_005fmake_005fstructv"><span class="category-def">C Function: </span><span><code class="def-type">SCM</code> <strong class="def-name">scm_c_make_structv</strong> <code class="def-code-arguments">(SCM vtable, SCM tail_size, size_t n_inits, scm_t_bits init[])</code><a class="copiable-link" href="#index-scm_005fc_005fmake_005fstructv"> ¶</a></span></dt>
|
||
|
<dd><p>There are a few ways to make structures from C. <code class="code">scm_make_struct</code>
|
||
|
takes a list, <code class="code">scm_c_make_struct</code> takes variable arguments
|
||
|
terminated with SCM_UNDEFINED, and <code class="code">scm_c_make_structv</code> takes a
|
||
|
packed array.
|
||
|
</p>
|
||
|
<p>For all of these, <var class="var">tail_size</var> should be zero (as a SCM value).
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-struct_003f"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">struct?</strong> <var class="def-var-arguments">obj</var><a class="copiable-link" href="#index-struct_003f"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fstruct_005fp"><span class="category-def">C Function: </span><span><strong class="def-name">scm_struct_p</strong> <var class="def-var-arguments">(obj)</var><a class="copiable-link" href="#index-scm_005fstruct_005fp"> ¶</a></span></dt>
|
||
|
<dd><p>Return <code class="code">#t</code> if <var class="var">obj</var> is a structure, or <code class="code">#f</code> if not.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-struct_002dref"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">struct-ref</strong> <var class="def-var-arguments">struct n</var><a class="copiable-link" href="#index-struct_002dref"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fstruct_005fref"><span class="category-def">C Function: </span><span><strong class="def-name">scm_struct_ref</strong> <var class="def-var-arguments">(struct, n)</var><a class="copiable-link" href="#index-scm_005fstruct_005fref"> ¶</a></span></dt>
|
||
|
<dd><p>Return the contents of field number <var class="var">n</var> in <var class="var">struct</var>. The
|
||
|
first field is number 0.
|
||
|
</p>
|
||
|
<p>An error is thrown if <var class="var">n</var> is out of range.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-struct_002dset_0021"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">struct-set!</strong> <var class="def-var-arguments">struct n value</var><a class="copiable-link" href="#index-struct_002dset_0021"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fstruct_005fset_005fx"><span class="category-def">C Function: </span><span><strong class="def-name">scm_struct_set_x</strong> <var class="def-var-arguments">(struct, n, value)</var><a class="copiable-link" href="#index-scm_005fstruct_005fset_005fx"> ¶</a></span></dt>
|
||
|
<dd><p>Set field number <var class="var">n</var> in <var class="var">struct</var> to <var class="var">value</var>. The first
|
||
|
field is number 0.
|
||
|
</p>
|
||
|
<p>An error is thrown if <var class="var">n</var> is out of range, or if the field cannot
|
||
|
be written because it’s <code class="code">r</code> read-only.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<p>Unboxed fields (those with type <code class="code">u</code>) need to be accessed with
|
||
|
special procedures.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-struct_002dref_002funboxed"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">struct-ref/unboxed</strong> <var class="def-var-arguments">struct n</var><a class="copiable-link" href="#index-struct_002dref_002funboxed"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-struct_002dset_0021_002funboxed"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">struct-set!/unboxed</strong> <var class="def-var-arguments">struct n value</var><a class="copiable-link" href="#index-struct_002dset_0021_002funboxed"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fstruct_005fref_005funboxed"><span class="category-def">C Function: </span><span><strong class="def-name">scm_struct_ref_unboxed</strong> <var class="def-var-arguments">(struct, n)</var><a class="copiable-link" href="#index-scm_005fstruct_005fref_005funboxed"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fstruct_005fset_005fx_005funboxed"><span class="category-def">C Function: </span><span><strong class="def-name">scm_struct_set_x_unboxed</strong> <var class="def-var-arguments">(struct, n, value)</var><a class="copiable-link" href="#index-scm_005fstruct_005fset_005fx_005funboxed"> ¶</a></span></dt>
|
||
|
<dd><p>Like <code class="code">struct-ref</code> and <code class="code">struct-set!</code>, except that these may
|
||
|
only be used on unboxed fields. <code class="code">struct-ref/unboxed</code> will always
|
||
|
return a positive integer. Likewise, <code class="code">struct-set!/unboxed</code> takes
|
||
|
an unsigned integer as the <var class="var">value</var> argument, and will signal an
|
||
|
error otherwise.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-struct_002dvtable"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">struct-vtable</strong> <var class="def-var-arguments">struct</var><a class="copiable-link" href="#index-struct_002dvtable"> ¶</a></span></dt>
|
||
|
<dt class="deffnx def-cmd-deffn" id="index-scm_005fstruct_005fvtable"><span class="category-def">C Function: </span><span><strong class="def-name">scm_struct_vtable</strong> <var class="def-var-arguments">(struct)</var><a class="copiable-link" href="#index-scm_005fstruct_005fvtable"> ¶</a></span></dt>
|
||
|
<dd><p>Return the vtable that describes <var class="var">struct</var>.
|
||
|
</p>
|
||
|
<p>The vtable is effectively the type of the structure. See <a class="ref" href="Vtable-Contents.html">Vtable Contents</a>, for more on vtables.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
<hr>
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Vtable-Contents.html">Vtable Contents</a>, Previous: <a href="Vtables.html">Vtables</a>, Up: <a href="Structures.html">Structures</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
</body>
|
||
|
</html>
|