157 lines
8.1 KiB
HTML
157 lines
8.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>Metaobjects and the Metaobject Protocol (Guile Reference Manual)</title>
|
|
|
|
<meta name="description" content="Metaobjects and the Metaobject Protocol (Guile Reference Manual)">
|
|
<meta name="keywords" content="Metaobjects and the Metaobject 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="Metaclasses.html" rel="next" title="Metaclasses">
|
|
<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="Metaobjects-and-the-Metaobject-Protocol">
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="Metaclasses.html" accesskey="n" rel="next">Metaclasses</a>, Up: <a href="The-Metaobject-Protocol.html" accesskey="u" rel="up">The Metaobject Protocol</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="subsection" id="Metaobjects-and-the-Metaobject-Protocol-1"><span>8.11.1 Metaobjects and the Metaobject Protocol<a class="copiable-link" href="#Metaobjects-and-the-Metaobject-Protocol-1"> ¶</a></span></h4>
|
|
|
|
<p>The building blocks of GOOPS are classes, slot definitions, instances,
|
|
generic functions and methods. A class is a grouping of inheritance
|
|
relations and slot definitions. An instance is an object with slots
|
|
that are allocated following the rules implied by its class’s
|
|
superclasses and slot definitions. A generic function is a collection
|
|
of methods and rules for determining which of those methods to apply
|
|
when the generic function is invoked. A method is a procedure and a set
|
|
of specializers that specify the type of arguments to which the
|
|
procedure is applicable.
|
|
</p>
|
|
<p>Of these entities, GOOPS represents classes, generic functions and
|
|
methods as “metaobjects”. In other words, the values in a GOOPS
|
|
program that describe classes, generic functions and methods, are
|
|
themselves instances (or “objects”) of special GOOPS classes that
|
|
encapsulate the behavior, respectively, of classes, generic functions,
|
|
and methods.
|
|
</p>
|
|
<p>(The other two entities are slot definitions and instances. Slot
|
|
definitions are not strictly instances, but every slot definition is
|
|
associated with a GOOPS class that specifies the behavior of the slot
|
|
as regards accessibility and protection from garbage collection.
|
|
Instances are of course objects in the usual sense, and there is no
|
|
benefit from thinking of them as metaobjects.)
|
|
</p>
|
|
<p>The “metaobject protocol” (or “MOP”) is the specification of the
|
|
generic functions which determine the behavior of these metaobjects and
|
|
the circumstances in which these generic functions are invoked.
|
|
</p>
|
|
<p>For a concrete example of what this means, consider how GOOPS calculates
|
|
the set of slots for a class that is being defined using
|
|
<code class="code">define-class</code>. The desired set of slots is the union of the new
|
|
class’s direct slots and the slots of all its superclasses. But
|
|
<code class="code">define-class</code> itself does not perform this calculation. Instead,
|
|
there is a method of the <code class="code">initialize</code> generic function that is
|
|
specialized for instances of type <code class="code"><class></code>, and it is this method
|
|
that performs the slot calculation.
|
|
</p>
|
|
<p><code class="code">initialize</code> is a generic function which GOOPS calls whenever a new
|
|
instance is created, immediately after allocating memory for a new
|
|
instance, in order to initialize the new instance’s slots. The sequence
|
|
of steps is as follows.
|
|
</p>
|
|
<ul class="itemize mark-bullet">
|
|
<li><code class="code">define-class</code> uses <code class="code">make</code> to make a new instance of the
|
|
<code class="code"><class></code> class, passing as initialization arguments the
|
|
superclasses, slot definitions and class options that were specified in
|
|
the <code class="code">define-class</code> form.
|
|
|
|
</li><li><code class="code">make</code> allocates memory for the new instance, and invokes the
|
|
<code class="code">initialize</code> generic function to initialize the new instance’s
|
|
slots.
|
|
|
|
</li><li>The <code class="code">initialize</code> generic function applies the method that is
|
|
specialized for instances of type <code class="code"><class></code>, and this method
|
|
performs the slot calculation.
|
|
</li></ul>
|
|
|
|
<p>In other words, rather than being hardcoded in <code class="code">define-class</code>, the
|
|
default behavior of class definition is encapsulated by generic
|
|
function methods that are specialized for the class <code class="code"><class></code>.
|
|
</p>
|
|
<p>It is possible to create a new class that inherits from <code class="code"><class></code>,
|
|
which is called a “metaclass”, and to write a new <code class="code">initialize</code>
|
|
method that is specialized for instances of the new metaclass. Then, if
|
|
the <code class="code">define-class</code> form includes a <code class="code">#:metaclass</code> class option
|
|
whose value is the new metaclass, the class that is defined by the
|
|
<code class="code">define-class</code> form will be an instance of the new metaclass rather
|
|
than of the default <code class="code"><class></code>, and will be defined in accordance
|
|
with the new <code class="code">initialize</code> method. Thus the default slot
|
|
calculation, as well as any other aspect of the new class’s relationship
|
|
with its superclasses, can be modified or overridden.
|
|
</p>
|
|
<p>In a similar way, the behavior of generic functions can be modified or
|
|
overridden by creating a new class that inherits from the standard
|
|
generic function class <code class="code"><generic></code>, writing appropriate methods
|
|
that are specialized to the new class, and creating new generic
|
|
functions that are instances of the new class.
|
|
</p>
|
|
<p>The same is true for method metaobjects. And the same basic mechanism
|
|
allows the application class author to write an <code class="code">initialize</code> method
|
|
that is specialized to their application class, to initialize instances
|
|
of that class.
|
|
</p>
|
|
<p>Such is the power of the MOP. Note that <code class="code">initialize</code> is just one
|
|
of a large number of generic functions that can be customized to modify
|
|
the behavior of application objects and classes and of GOOPS itself.
|
|
Each following section covers a particular area of GOOPS functionality,
|
|
and describes the generic functions that are relevant for customization
|
|
of that area.
|
|
</p>
|
|
</div>
|
|
<hr>
|
|
<div class="nav-panel">
|
|
<p>
|
|
Next: <a href="Metaclasses.html">Metaclasses</a>, Up: <a href="The-Metaobject-Protocol.html">The Metaobject Protocol</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>
|