1
0
Fork 0
cl-sites/guile.html_node/Instance-Creation.html
2024-12-17 12:49:28 +01:00

140 lines
7 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>Instance Creation (Guile Reference Manual)</title>
<meta name="description" content="Instance Creation (Guile Reference Manual)">
<meta name="keywords" content="Instance Creation (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="GOOPS.html" rel="up" title="GOOPS">
<link href="Slot-Options.html" rel="next" title="Slot Options">
<link href="Class-Definition.html" rel="prev" title="Class Definition">
<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="section-level-extent" id="Instance-Creation">
<div class="nav-panel">
<p>
Next: <a href="Slot-Options.html" accesskey="n" rel="next">Slot Options</a>, Previous: <a href="Class-Definition.html" accesskey="p" rel="prev">Class Definition</a>, Up: <a href="GOOPS.html" accesskey="u" rel="up">GOOPS</a> &nbsp; [<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>
<h3 class="section" id="Instance-Creation-and-Slot-Access"><span>8.3 Instance Creation and Slot Access<a class="copiable-link" href="#Instance-Creation-and-Slot-Access"> &para;</a></span></h3>
<p>An instance (or object) of a defined class can be created with
<code class="code">make</code>. <code class="code">make</code> takes one mandatory parameter, which is the
class of the instance to create, and a list of optional arguments that
will be used to initialize the slots of the new instance. For instance
the following form
</p>
<a class="index-entry-id" id="index-make"></a>
<a class="index-entry-id" id="index-instance"></a>
<div class="example lisp">
<pre class="lisp-preformatted">(define c (make &lt;my-complex&gt;))
</pre></div>
<p>creates a new <code class="code">&lt;my-complex&gt;</code> object and binds it to the Scheme
variable <code class="code">c</code>.
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-make-1"><span class="category-def">generic: </span><span><strong class="def-name">make</strong><a class="copiable-link" href="#index-make-1"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-make-2"><span class="category-def">method: </span><span><strong class="def-name">make</strong> <var class="def-var-arguments">(class &lt;class&gt;) initarg &hellip;</var><a class="copiable-link" href="#index-make-2"> &para;</a></span></dt>
<dd><p>Create and return a new instance of class <var class="var">class</var>, initialized using
<var class="var">initarg</var> <small class="enddots">...</small>.
</p>
<p>In theory, <var class="var">initarg</var> &hellip; can have any structure that is
understood by whatever methods get applied when the <code class="code">initialize</code>
generic function is applied to the newly allocated instance.
</p>
<p>In practice, specialized <code class="code">initialize</code> methods would normally call
<code class="code">(next-method)</code>, and so eventually the standard GOOPS
<code class="code">initialize</code> methods are applied. These methods expect
<var class="var">initargs</var> to be a list with an even number of elements, where
even-numbered elements (counting from zero) are keywords and
odd-numbered elements are the corresponding values.
</p>
<p>GOOPS processes initialization argument keywords automatically for slots
whose definition includes the <code class="code">#:init-keyword</code> option (see <a class="pxref" href="Slot-Options.html">init-keyword</a>). Other keyword value pairs can only be
processed by an <code class="code">initialize</code> method that is specialized for the new
instance&rsquo;s class. Any unprocessed keyword value pairs are ignored.
</p></dd></dl>
<dl class="first-deffn">
<dt class="deffn" id="index-make_002dinstance"><span class="category-def">generic: </span><span><strong class="def-name">make-instance</strong><a class="copiable-link" href="#index-make_002dinstance"> &para;</a></span></dt>
<dt class="deffnx def-cmd-deffn" id="index-make_002dinstance-1"><span class="category-def">method: </span><span><strong class="def-name">make-instance</strong> <var class="def-var-arguments">(class &lt;class&gt;) initarg &hellip;</var><a class="copiable-link" href="#index-make_002dinstance-1"> &para;</a></span></dt>
<dd><p><code class="code">make-instance</code> is an alias for <code class="code">make</code>.
</p></dd></dl>
<p>The slots of the new complex number can be accessed using
<code class="code">slot-ref</code> and <code class="code">slot-set!</code>. <code class="code">slot-set!</code> sets the value
of an object slot and <code class="code">slot-ref</code> retrieves it.
</p>
<a class="index-entry-id" id="index-slot_002dset_0021"></a>
<a class="index-entry-id" id="index-slot_002dref"></a>
<div class="example lisp">
<div class="group"><pre class="lisp-preformatted">(slot-set! c 'r 10)
(slot-set! c 'i 3)
(slot-ref c 'r) &rArr; 10
(slot-ref c 'i) &rArr; 3
</pre></div></div>
<p>The <code class="code">(oop goops describe)</code> module provides a <code class="code">describe</code>
function that is useful for seeing all the slots of an object; it prints
the slots and their values to standard output.
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(describe c)
-|
#&lt;&lt;my-complex&gt; 401d8638&gt; is an instance of class &lt;my-complex&gt;
Slots are:
r = 10
i = 3
</pre></div>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Slot-Options.html">Slot Options</a>, Previous: <a href="Class-Definition.html">Class Definition</a>, Up: <a href="GOOPS.html">GOOPS</a> &nbsp; [<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>