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

144 lines
7.1 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 Protocol (Guile Reference Manual)</title>
<meta name="description" content="Instance Creation Protocol (Guile Reference Manual)">
<meta name="keywords" content="Instance Creation Protocol (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="The-Metaobject-Protocol.html" rel="up" title="The Metaobject Protocol">
<link href="Class-Definition-Protocol.html" rel="next" title="Class Definition Protocol">
<link href="MOP-Specification.html" rel="prev" title="MOP Specification">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Instance-Creation-Protocol">
<div class="nav-panel">
<p>
Next: <a href="Class-Definition-Protocol.html" accesskey="n" rel="next">Class Definition Protocol</a>, Previous: <a href="MOP-Specification.html" accesskey="p" rel="prev">MOP Specification</a>, Up: <a href="The-Metaobject-Protocol.html" accesskey="u" rel="up">The Metaobject Protocol</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>
<h4 class="subsection" id="Instance-Creation-Protocol-1"><span>8.11.4 Instance Creation Protocol<a class="copiable-link" href="#Instance-Creation-Protocol-1"> &para;</a></span></h4>
<p><code class="code">make &lt;class&gt; . <var class="var">initargs</var></code> (method)
</p>
<ul class="itemize mark-bullet">
<li><code class="code">allocate-instance <var class="var">class</var> <var class="var">initargs</var></code> (generic)
<p>The applied <code class="code">allocate-instance</code> method should allocate storage for
a new instance of class <var class="var">class</var> and return the uninitialized instance.
</p>
</li><li><code class="code">initialize <var class="var">instance</var> <var class="var">initargs</var></code> (generic)
<p><var class="var">instance</var> is the uninitialized instance returned by
<code class="code">allocate-instance</code>. The applied method should initialize the new
instance in whatever sense is appropriate for its class. The method&rsquo;s
return value is ignored.
</p></li></ul>
<p><code class="code">make</code> itself is a generic function. Hence the <code class="code">make</code>
invocation itself can be customized in the case where the new instance&rsquo;s
metaclass is more specialized than the default <code class="code">&lt;class&gt;</code>, by
defining a <code class="code">make</code> method that is specialized to that metaclass.
</p>
<p>Normally, however, the method for classes with metaclass <code class="code">&lt;class&gt;</code>
will be applied. This method calls two generic functions:
</p>
<ul class="itemize mark-bullet">
<li>(allocate-instance <var class="var">class</var> . <var class="var">initargs</var>)
</li><li>(initialize <var class="var">instance</var> . <var class="var">initargs</var>)
</li></ul>
<p><code class="code">allocate-instance</code> allocates storage for and returns the new
instance, uninitialized. You might customize <code class="code">allocate-instance</code>,
for example, if you wanted to provide a GOOPS wrapper around some other
object programming system.
</p>
<p>To do this, you would create a specialized metaclass, which would act as
the metaclass for all classes and instances from the other system. Then
define an <code class="code">allocate-instance</code> method, specialized to that
metaclass, which calls a Guile primitive C function (or FFI code), which
in turn allocates the new instance using the interface of the other
object system.
</p>
<p>In this case, for a complete system, you would also need to customize a
number of other generic functions like <code class="code">make</code> and
<code class="code">initialize</code>, so that GOOPS knows how to make classes from the
other system, access instance slots, and so on.
</p>
<p><code class="code">initialize</code> initializes the instance that is returned by
<code class="code">allocate-instance</code>. The standard GOOPS methods perform
initializations appropriate to the instance class.
</p>
<ul class="itemize mark-bullet">
<li>At the least specialized level, the method for instances of type
<code class="code">&lt;object&gt;</code> performs internal GOOPS instance initialization, and
initializes the instance&rsquo;s slots according to the slot definitions and
any slot initialization keywords that appear in <var class="var">initargs</var>.
</li><li>The method for instances of type <code class="code">&lt;class&gt;</code> calls
<code class="code">(next-method)</code>, then performs the class initializations described
in <a class="ref" href="Class-Definition-Protocol.html">Class Definition Protocol</a>.
</li><li>and so on for generic functions, methods, operator classes &hellip;
</li></ul>
<p>Similarly, you can customize the initialization of instances of any
application-defined class by defining an <code class="code">initialize</code> method
specialized to that class.
</p>
<p>Imagine a class whose instances&rsquo; slots need to be initialized at
instance creation time by querying a database. Although it might be
possible to achieve this a combination of <code class="code">#:init-thunk</code> keywords
and closures in the slot definitions, it may be neater to write an
<code class="code">initialize</code> method for the class that queries the database once
and initializes all the dependent slot values according to the results.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Class-Definition-Protocol.html">Class Definition Protocol</a>, Previous: <a href="MOP-Specification.html">MOP Specification</a>, Up: <a href="The-Metaobject-Protocol.html">The Metaobject Protocol</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>