170 lines
9 KiB
HTML
170 lines
9 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>Class Definition (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="Class Definition (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="Class Definition (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="Instance-Creation.html" rel="next" title="Instance Creation">
|
||
|
<link href="Copyright-Notice.html" rel="prev" title="Copyright Notice">
|
||
|
<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="Class-Definition">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Instance-Creation.html" accesskey="n" rel="next">Instance Creation and Slot Access</a>, Previous: <a href="Copyright-Notice.html" accesskey="p" rel="prev">Copyright Notice</a>, Up: <a href="GOOPS.html" accesskey="u" rel="up">GOOPS</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>
|
||
|
<h3 class="section" id="Class-Definition-1"><span>8.2 Class Definition<a class="copiable-link" href="#Class-Definition-1"> ¶</a></span></h3>
|
||
|
|
||
|
<p>A new class is defined with the <code class="code">define-class</code> syntax:
|
||
|
</p>
|
||
|
<a class="index-entry-id" id="index-define_002dclass"></a>
|
||
|
<a class="index-entry-id" id="index-class"></a>
|
||
|
<div class="example lisp">
|
||
|
<pre class="lisp-preformatted">(define-class <var class="var">class</var> (<var class="var">superclass</var> ...)
|
||
|
<var class="var">slot-description</var> ...
|
||
|
<var class="var">class-option</var> ...)
|
||
|
</pre></div>
|
||
|
|
||
|
<p><var class="var">class</var> is the class being defined. The list of <var class="var">superclass</var>es
|
||
|
specifies which existing classes, if any, to inherit slots and
|
||
|
properties from. <em class="dfn">Slots</em> hold per-instance<a class="footnote" id="DOCF29" href="#FOOT29"><sup>29</sup></a> data, for instances of
|
||
|
that class — like “fields” or “member variables” in other object
|
||
|
oriented systems. Each <var class="var">slot-description</var> gives the name of a slot
|
||
|
and optionally some “properties” of this slot; for example its initial
|
||
|
value, the name of a function which will access its value, and so on.
|
||
|
Class options, slot descriptions and inheritance are discussed more
|
||
|
below.
|
||
|
<a class="index-entry-id" id="index-slot"></a>
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-define_002dclass-1"><span class="category-def">syntax: </span><span><strong class="def-name">define-class</strong> <var class="def-var-arguments">name (super …) slot-definition … class-option …</var><a class="copiable-link" href="#index-define_002dclass-1"> ¶</a></span></dt>
|
||
|
<dd><p>Define a class called <var class="var">name</var> that inherits from <var class="var">super</var>s, with
|
||
|
direct slots defined by <var class="var">slot-definition</var>s and <var class="var">class-option</var>s.
|
||
|
The newly created class is bound to the variable name <var class="var">name</var> in the
|
||
|
current environment.
|
||
|
</p>
|
||
|
<p>Each <var class="var">slot-definition</var> is either a symbol that names the slot or a
|
||
|
list,
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(<var class="var">slot-name-symbol</var> . <var class="var">slot-options</var>)
|
||
|
</pre></div>
|
||
|
|
||
|
<p>where <var class="var">slot-name-symbol</var> is a symbol and <var class="var">slot-options</var> is a
|
||
|
list with an even number of elements. The even-numbered elements of
|
||
|
<var class="var">slot-options</var> (counting from zero) are slot option keywords; the
|
||
|
odd-numbered elements are the corresponding values for those keywords.
|
||
|
</p>
|
||
|
<p>Each <var class="var">class-option</var> is an option keyword and corresponding value.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<p>As an example, let us define a type for representing a complex number
|
||
|
in terms of two real numbers.<a class="footnote" id="DOCF30" href="#FOOT30"><sup>30</sup></a> This can be done with the following class definition:
|
||
|
</p>
|
||
|
<div class="example lisp">
|
||
|
<pre class="lisp-preformatted">(define-class <my-complex> (<number>)
|
||
|
r i)
|
||
|
</pre></div>
|
||
|
|
||
|
<p>This binds the variable <code class="code"><my-complex></code> to a new class whose
|
||
|
instances will contain two slots. These slots are called <code class="code">r</code> and
|
||
|
<code class="code">i</code> and will hold the real and imaginary parts of a complex
|
||
|
number. Note that this class inherits from <code class="code"><number></code>, which is a
|
||
|
predefined class.<a class="footnote" id="DOCF31" href="#FOOT31"><sup>31</sup></a>
|
||
|
</p>
|
||
|
<p>Slot options are described in the next section. The possible class
|
||
|
options are as follows.
|
||
|
</p>
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-_0023_003ametaclass"><span class="category-def">class option: </span><span><strong class="def-name">#:metaclass</strong> <var class="def-var-arguments">metaclass</var><a class="copiable-link" href="#index-_0023_003ametaclass"> ¶</a></span></dt>
|
||
|
<dd><p>The <code class="code">#:metaclass</code> class option specifies the metaclass of the class
|
||
|
being defined. <var class="var">metaclass</var> must be a class that inherits from
|
||
|
<code class="code"><class></code>. For the use of metaclasses, see <a class="ref" href="Metaobjects-and-the-Metaobject-Protocol.html">Metaobjects and the Metaobject Protocol</a> and <a class="ref" href="Metaclasses.html">Metaclasses</a>.
|
||
|
</p>
|
||
|
<p>If the <code class="code">#:metaclass</code> option is absent, GOOPS reuses or constructs a
|
||
|
metaclass for the new class by calling <code class="code">ensure-metaclass</code>
|
||
|
(see <a class="pxref" href="Class-Definition-Protocol.html">ensure-metaclass</a>).
|
||
|
</p></dd></dl>
|
||
|
|
||
|
<dl class="first-deffn">
|
||
|
<dt class="deffn" id="index-_0023_003aname"><span class="category-def">class option: </span><span><strong class="def-name">#:name</strong> <var class="def-var-arguments">name</var><a class="copiable-link" href="#index-_0023_003aname"> ¶</a></span></dt>
|
||
|
<dd><p>The <code class="code">#:name</code> class option specifies the new class’s name. This
|
||
|
name is used to identify the class whenever related objects - the class
|
||
|
itself, its instances and its subclasses - are printed.
|
||
|
</p>
|
||
|
<p>If the <code class="code">#:name</code> option is absent, GOOPS uses the first argument to
|
||
|
<code class="code">define-class</code> as the class name.
|
||
|
</p></dd></dl>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
<div class="footnotes-segment">
|
||
|
<hr>
|
||
|
<h4 class="footnotes-heading">Footnotes</h4>
|
||
|
|
||
|
<h5 class="footnote-body-heading"><a id="FOOT29" href="#DOCF29">(29)</a></h5>
|
||
|
<p>Usually — but
|
||
|
see also the <code class="code">#:allocation</code> slot option.</p>
|
||
|
<h5 class="footnote-body-heading"><a id="FOOT30" href="#DOCF30">(30)</a></h5>
|
||
|
<p>Of course Guile already
|
||
|
provides complex numbers, and <code class="code"><complex></code> is in fact a predefined
|
||
|
class in GOOPS; but the definition here is still useful as an
|
||
|
example.</p>
|
||
|
<h5 class="footnote-body-heading"><a id="FOOT31" href="#DOCF31">(31)</a></h5>
|
||
|
<p><code class="code"><number></code> is the direct superclass of
|
||
|
the predefined class <code class="code"><complex></code>; <code class="code"><complex></code> is the
|
||
|
superclass of <code class="code"><real></code>, and <code class="code"><real></code> is the superclass of
|
||
|
<code class="code"><integer></code>.</p>
|
||
|
</div>
|
||
|
<hr>
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Next: <a href="Instance-Creation.html">Instance Creation and Slot Access</a>, Previous: <a href="Copyright-Notice.html">Copyright Notice</a>, Up: <a href="GOOPS.html">GOOPS</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>
|