Debian Codegraph @

inst/doc/S7.html

raw symbols SCIP
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13
14
15 <title>S7 basics</title>
16
17 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
18 // be compatible with the behavior of Pandoc < 2.8).
19 document.addEventListener('DOMContentLoaded', function(e) {
20 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
21 var i, h, a;
22 for (i = 0; i < hs.length; i++) {
23 h = hs[i];
24 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
25 a = h.attributes;
26 while (a.length > 0) h.removeAttribute(a[0].name);
27 }
28 });
29 </script>
30
31 <style type="text/css">
32 code{white-space: pre-wrap;}
33 span.smallcaps{font-variant: small-caps;}
34 span.underline{text-decoration: underline;}
35 div.column{display: inline-block; vertical-align: top; width: 50%;}
36 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
37 ul.task-list{list-style: none;}
38 </style>
39
40
41
42 <style type="text/css">
43 code {
44 white-space: pre;
45 }
46 .sourceCode {
47 overflow: visible;
48 }
49 </style>
50 <style type="text/css" data-origin="pandoc">
51 html { -webkit-text-size-adjust: 100%; }
52 pre > code.sourceCode { white-space: pre; position: relative; }
53 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
54 pre > code.sourceCode > span:empty { height: 1.2em; }
55 .sourceCode { overflow: visible; }
56 code.sourceCode > span { color: inherit; text-decoration: inherit; }
57 div.sourceCode { margin: 1em 0; }
58 pre.sourceCode { margin: 0; }
59 @media screen {
60 div.sourceCode { overflow: auto; }
61 }
62 @media print {
63 pre > code.sourceCode { white-space: pre-wrap; }
64 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
65 }
66 pre.numberSource code
67 { counter-reset: source-line 0; }
68 pre.numberSource code > span
69 { position: relative; left: -4em; counter-increment: source-line; }
70 pre.numberSource code > span > a:first-child::before
71 { content: counter(source-line);
72 position: relative; left: -1em; text-align: right; vertical-align: baseline;
73 border: none; display: inline-block;
74 -webkit-touch-callout: none; -webkit-user-select: none;
75 -khtml-user-select: none; -moz-user-select: none;
76 -ms-user-select: none; user-select: none;
77 padding: 0 4px; width: 4em;
78 color: #aaaaaa;
79 }
80 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
81 div.sourceCode
82 { }
83 @media screen {
84 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
85 }
86 code span.al { color: #ff0000; font-weight: bold; }
87 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
88 code span.at { color: #7d9029; }
89 code span.bn { color: #40a070; }
90 code span.bu { color: #008000; }
91 code span.cf { color: #007020; font-weight: bold; }
92 code span.ch { color: #4070a0; }
93 code span.cn { color: #880000; }
94 code span.co { color: #60a0b0; font-style: italic; }
95 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
96 code span.do { color: #ba2121; font-style: italic; }
97 code span.dt { color: #902000; }
98 code span.dv { color: #40a070; }
99 code span.er { color: #ff0000; font-weight: bold; }
100 code span.ex { }
101 code span.fl { color: #40a070; }
102 code span.fu { color: #06287e; }
103 code span.im { color: #008000; font-weight: bold; }
104 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
105 code span.kw { color: #007020; font-weight: bold; }
106 code span.op { color: #666666; }
107 code span.ot { color: #007020; }
108 code span.pp { color: #bc7a00; }
109 code span.sc { color: #4070a0; }
110 code span.ss { color: #bb6688; }
111 code span.st { color: #4070a0; }
112 code span.va { color: #19177c; }
113 code span.vs { color: #4070a0; }
114 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
115 </style>
116 <script>
117 // apply pandoc div.sourceCode style to pre.sourceCode instead
118 (function() {
119 var sheets = document.styleSheets;
120 for (var i = 0; i < sheets.length; i++) {
121 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
122 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
123 var j = 0;
124 while (j < rules.length) {
125 var rule = rules[j];
126 // check if there is a div.sourceCode rule
127 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
128 j++;
129 continue;
130 }
131 var style = rule.style.cssText;
132 // check if color or background-color is set
133 if (rule.style.color === '' && rule.style.backgroundColor === '') {
134 j++;
135 continue;
136 }
137 // replace div.sourceCode by a pre.sourceCode rule
138 sheets[i].deleteRule(j);
139 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
140 }
141 }
142 })();
143 </script>
144
145
146
147
148 <style type="text/css">body {
149 background-color: #fff;
150 margin: 1em auto;
151 max-width: 700px;
152 overflow: visible;
153 padding-left: 2em;
154 padding-right: 2em;
155 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
156 font-size: 14px;
157 line-height: 1.35;
158 }
159 #TOC {
160 clear: both;
161 margin: 0 0 10px 10px;
162 padding: 4px;
163 width: 400px;
164 border: 1px solid #CCCCCC;
165 border-radius: 5px;
166 background-color: #f6f6f6;
167 font-size: 13px;
168 line-height: 1.3;
169 }
170 #TOC .toctitle {
171 font-weight: bold;
172 font-size: 15px;
173 margin-left: 5px;
174 }
175 #TOC ul {
176 padding-left: 40px;
177 margin-left: -1.5em;
178 margin-top: 5px;
179 margin-bottom: 5px;
180 }
181 #TOC ul ul {
182 margin-left: -2em;
183 }
184 #TOC li {
185 line-height: 16px;
186 }
187 table {
188 margin: 1em auto;
189 border-width: 1px;
190 border-color: #DDDDDD;
191 border-style: outset;
192 border-collapse: collapse;
193 }
194 table th {
195 border-width: 2px;
196 padding: 5px;
197 border-style: inset;
198 }
199 table td {
200 border-width: 1px;
201 border-style: inset;
202 line-height: 18px;
203 padding: 5px 5px;
204 }
205 table, table th, table td {
206 border-left-style: none;
207 border-right-style: none;
208 }
209 table thead, table tr.even {
210 background-color: #f7f7f7;
211 }
212 p {
213 margin: 0.5em 0;
214 }
215 blockquote {
216 background-color: #f6f6f6;
217 padding: 0.25em 0.75em;
218 }
219 hr {
220 border-style: solid;
221 border: none;
222 border-top: 1px solid #777;
223 margin: 28px 0;
224 }
225 dl {
226 margin-left: 0;
227 }
228 dl dd {
229 margin-bottom: 13px;
230 margin-left: 13px;
231 }
232 dl dt {
233 font-weight: bold;
234 }
235 ul {
236 margin-top: 0;
237 }
238 ul li {
239 list-style: circle outside;
240 }
241 ul ul {
242 margin-bottom: 0;
243 }
244 pre, code {
245 background-color: #f7f7f7;
246 border-radius: 3px;
247 color: #333;
248 white-space: pre-wrap;
249 }
250 pre {
251 border-radius: 3px;
252 margin: 5px 0px 10px 0px;
253 padding: 10px;
254 }
255 pre:not([class]) {
256 background-color: #f7f7f7;
257 }
258 code {
259 font-family: Consolas, Monaco, 'Courier New', monospace;
260 font-size: 85%;
261 }
262 p > code, li > code {
263 padding: 2px 0px;
264 }
265 div.figure {
266 text-align: center;
267 }
268 img {
269 background-color: #FFFFFF;
270 padding: 2px;
271 border: 1px solid #DDDDDD;
272 border-radius: 3px;
273 border: 1px solid #CCCCCC;
274 margin: 0 5px;
275 }
276 h1 {
277 margin-top: 0;
278 font-size: 35px;
279 line-height: 40px;
280 }
281 h2 {
282 border-bottom: 4px solid #f7f7f7;
283 padding-top: 10px;
284 padding-bottom: 2px;
285 font-size: 145%;
286 }
287 h3 {
288 border-bottom: 2px solid #f7f7f7;
289 padding-top: 10px;
290 font-size: 120%;
291 }
292 h4 {
293 border-bottom: 1px solid #f7f7f7;
294 margin-left: 8px;
295 font-size: 105%;
296 }
297 h5, h6 {
298 border-bottom: 1px solid #ccc;
299 font-size: 105%;
300 }
301 a {
302 color: #0033dd;
303 text-decoration: none;
304 }
305 a:hover {
306 color: #6666ff; }
307 a:visited {
308 color: #800080; }
309 a:visited:hover {
310 color: #BB00BB; }
311 a[href^="http:"] {
312 text-decoration: underline; }
313 a[href^="https:"] {
314 text-decoration: underline; }
315
316 code > span.kw { color: #555; font-weight: bold; }
317 code > span.dt { color: #902000; }
318 code > span.dv { color: #40a070; }
319 code > span.bn { color: #d14; }
320 code > span.fl { color: #d14; }
321 code > span.ch { color: #d14; }
322 code > span.st { color: #d14; }
323 code > span.co { color: #888888; font-style: italic; }
324 code > span.ot { color: #007020; }
325 code > span.al { color: #ff0000; font-weight: bold; }
326 code > span.fu { color: #900; font-weight: bold; }
327 code > span.er { color: #a61717; background-color: #e3d2d2; }
328 </style>
329
330
331
332
333 </head>
334
335 <body>
336
337
338
339
340 <h1 class="title toc-ignore">S7 basics</h1>
341
342
343
344 <p>The S7 package provides a new OOP system designed to be a successor
345 to S3 and S4. It has been designed and implemented collaboratively by
346 the RConsortium Object-Oriented Programming Working Group, which
347 includes representatives from R-Core, BioConductor, RStudio/tidyverse,
348 and the wider R community.</p>
349 <p>This vignette gives an overview of the most important parts of S7:
350 classes and objects, generics and methods, and the basics of method
351 dispatch and inheritance.</p>
352 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(S7)</span></code></pre></div>
353 <div id="classes-and-objects" class="section level2">
354 <h2>Classes and objects</h2>
355 <p>S7 classes have a formal definition that you create with
356 <code>new_class()</code>. There are two arguments that you’ll use with
357 almost every class:</p>
358 <ul>
359 <li>The <code>name</code> of the class, supplied in the first
360 argument.</li>
361 <li>The class <code>properties</code>, the data associated with each
362 instance of the class. The easiest way to define properties is to supply
363 a named list where the values define the valid types of the
364 property.</li>
365 </ul>
366 <p>The following code defines a simple <code>dog</code> class with two
367 properties: a character <code>name</code> and a numeric
368 <code>age</code>.</p>
369 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>Dog <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Dog&quot;</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span>
370 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> <span class="at">name =</span> class_character,</span>
371 <span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> <span class="at">age =</span> class_numeric</span>
372 <span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a>))</span>
373 <span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a>Dog</span>
374 <span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#&gt; &lt;Dog&gt; class</span></span>
375 <span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#&gt; @ parent : &lt;S7_object&gt;</span></span>
376 <span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#&gt; @ constructor: function(name, age) {...}</span></span>
377 <span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#&gt; @ validator : &lt;NULL&gt;</span></span>
378 <span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#&gt; @ properties :</span></span>
379 <span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co">#&gt; $ name: &lt;character&gt; </span></span>
380 <span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co">#&gt; $ age : &lt;integer&gt; or &lt;double&gt;</span></span></code></pre></div>
381 <p>S7 provides a number of built-in definitions that allow you to refer
382 to existing base types that are not S7 classes. You can recognize these
383 definitions because they all start with <code>class_</code>.</p>
384 <p>Note that I’ve assigned the return value of <code>new_class()</code>
385 to an object with the same name as the class. This is important! That
386 object represents the class and is what you use to construct instances
387 of the class:</p>
388 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>lola <span class="ot">&lt;-</span> <span class="fu">Dog</span>(<span class="at">name =</span> <span class="st">&quot;Lola&quot;</span>, <span class="at">age =</span> <span class="dv">11</span>)</span>
389 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>lola</span>
390 <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#&gt; &lt;Dog&gt;</span></span>
391 <span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#&gt; @ name: chr &quot;Lola&quot;</span></span>
392 <span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#&gt; @ age : num 11</span></span></code></pre></div>
393 <p>Once you have an S7 object, you can get and set properties using
394 <code>@</code>:</p>
395 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>lola<span class="sc">@</span>age <span class="ot">&lt;-</span> <span class="dv">12</span></span>
396 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>lola<span class="sc">@</span>age</span>
397 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#&gt; [1] 12</span></span></code></pre></div>
398 <p>S7 automatically validates the type of the property using the type
399 supplied in <code>new_class()</code>:</p>
400 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>lola<span class="sc">@</span>age <span class="ot">&lt;-</span> <span class="st">&quot;twelve&quot;</span></span>
401 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co">#&gt; Error:</span></span>
402 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co">#&gt; ! &lt;Dog&gt;@age must be &lt;integer&gt; or &lt;double&gt;, not &lt;character&gt;</span></span></code></pre></div>
403 <p>Given an object, you can retrieve its class
404 <code>S7_class()</code>:</p>
405 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">S7_class</span>(lola)</span>
406 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#&gt; &lt;Dog&gt; class</span></span>
407 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#&gt; @ parent : &lt;S7_object&gt;</span></span>
408 <span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#&gt; @ constructor: function(name, age) {...}</span></span>
409 <span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co">#&gt; @ validator : &lt;NULL&gt;</span></span>
410 <span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#&gt; @ properties :</span></span>
411 <span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#&gt; $ name: &lt;character&gt; </span></span>
412 <span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#&gt; $ age : &lt;integer&gt; or &lt;double&gt;</span></span></code></pre></div>
413 <p>S7 objects also have an S3 <code>class()</code>. This is used for
414 compatibility with existing S3 generics and you can learn more about it
415 in <code>vignette(&quot;compatibility&quot;)</code>.</p>
416 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">class</span>(lola)</span>
417 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Dog&quot; &quot;S7_object&quot;</span></span></code></pre></div>
418 <p>If you want to learn more about the details of S7 classes and
419 objects, including validation methods and more details of properties,
420 please see <code>vignette(&quot;classes-objects&quot;)</code>.</p>
421 </div>
422 <div id="generics-and-methods" class="section level2">
423 <h2>Generics and methods</h2>
424 <p>S7, like S3 and S4, is built around the idea of <strong>generic
425 functions,</strong> or <strong>generics</strong> for short. A generic
426 defines an interface, which uses a different implementation depending on
427 the class of one or more arguments. The implementation for a specific
428 class is called a <strong>method</strong>, and the generic finds that
429 appropriate method by performing <strong>method dispatch</strong>.</p>
430 <p>Use <code>new_generic()</code> to create a S7 generic. In its
431 simplest form, it only needs two arguments: the name of the generic
432 (used in error messages) and the name of the argument used for method
433 dispatch:</p>
434 <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>speak <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;speak&quot;</span>, <span class="st">&quot;x&quot;</span>)</span></code></pre></div>
435 <p>Like with <code>new_class()</code>, you should always assign the
436 result of <code>new_generic()</code> to a variable with the same name as
437 the first argument.</p>
438 <p>Once you have a generic, you can register methods for specific
439 classes with
440 <code>method(generic, class) &lt;- implementation</code>.</p>
441 <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="fu">method</span>(speak, Dog) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
442 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> <span class="st">&quot;Woof&quot;</span></span>
443 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a>}</span></code></pre></div>
444 <p>Once the method is registered, the generic will use it when
445 appropriate:</p>
446 <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">speak</span>(lola)</span>
447 <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Woof&quot;</span></span></code></pre></div>
448 <p>Let’s define another class, this one for cats, and define another
449 method for <code>speak()</code>:</p>
450 <div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>Cat <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Cat&quot;</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span>
451 <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> <span class="at">name =</span> class_character,</span>
452 <span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="at">age =</span> class_double</span>
453 <span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a>))</span>
454 <span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="fu">method</span>(speak, Cat) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
455 <span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a> <span class="st">&quot;Meow&quot;</span></span>
456 <span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a>}</span>
457 <span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a></span>
458 <span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a>fluffy <span class="ot">&lt;-</span> <span class="fu">Cat</span>(<span class="at">name =</span> <span class="st">&quot;Fluffy&quot;</span>, <span class="at">age =</span> <span class="dv">5</span>)</span>
459 <span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="fu">speak</span>(fluffy)</span>
460 <span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Meow&quot;</span></span></code></pre></div>
461 <p>You get an error if you call the generic with a class that doesn’t
462 have a method:</p>
463 <div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="fu">speak</span>(<span class="dv">1</span>)</span>
464 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="co">#&gt; Error:</span></span>
465 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co">#&gt; ! Can&#39;t find method for `speak(&lt;double&gt;)`.</span></span></code></pre></div>
466 </div>
467 <div id="method-dispatch-and-inheritance" class="section level2">
468 <h2>Method dispatch and inheritance</h2>
469 <p>The <code>cat</code> and <code>dog</code> classes share the same
470 properties, so we could use a common parent class to extract out the
471 duplicated specification. We first define the parent class:</p>
472 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>Pet <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Pet&quot;</span>,</span>
473 <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span>
474 <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="at">name =</span> class_character,</span>
475 <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a> <span class="at">age =</span> class_numeric</span>
476 <span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a> )</span>
477 <span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a>)</span></code></pre></div>
478 <p>Then use the <code>parent</code> argument to
479 <code>new_class:</code></p>
480 <div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>Cat <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Cat&quot;</span>, <span class="at">parent =</span> Pet)</span>
481 <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a>Dog <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Dog&quot;</span>, <span class="at">parent =</span> Pet)</span>
482 <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a></span>
483 <span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a>Cat</span>
484 <span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#&gt; &lt;Cat&gt; class</span></span>
485 <span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#&gt; @ parent : &lt;Pet&gt;</span></span>
486 <span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#&gt; @ constructor: function(name, age) {...}</span></span>
487 <span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="co">#&gt; @ validator : &lt;NULL&gt;</span></span>
488 <span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="co">#&gt; @ properties :</span></span>
489 <span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co">#&gt; $ name: &lt;character&gt; </span></span>
490 <span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a><span class="co">#&gt; $ age : &lt;integer&gt; or &lt;double&gt;</span></span>
491 <span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a>Dog</span>
492 <span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="co">#&gt; &lt;Dog&gt; class</span></span>
493 <span id="cb14-14"><a href="#cb14-14" tabindex="-1"></a><span class="co">#&gt; @ parent : &lt;Pet&gt;</span></span>
494 <span id="cb14-15"><a href="#cb14-15" tabindex="-1"></a><span class="co">#&gt; @ constructor: function(name, age) {...}</span></span>
495 <span id="cb14-16"><a href="#cb14-16" tabindex="-1"></a><span class="co">#&gt; @ validator : &lt;NULL&gt;</span></span>
496 <span id="cb14-17"><a href="#cb14-17" tabindex="-1"></a><span class="co">#&gt; @ properties :</span></span>
497 <span id="cb14-18"><a href="#cb14-18" tabindex="-1"></a><span class="co">#&gt; $ name: &lt;character&gt; </span></span>
498 <span id="cb14-19"><a href="#cb14-19" tabindex="-1"></a><span class="co">#&gt; $ age : &lt;integer&gt; or &lt;double&gt;</span></span></code></pre></div>
499 <p>Because we have created new classes, we need to recreate the existing
500 <code>lola</code> and <code>fluffy</code> objects:</p>
501 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>lola <span class="ot">&lt;-</span> <span class="fu">Dog</span>(<span class="at">name =</span> <span class="st">&quot;Lola&quot;</span>, <span class="at">age =</span> <span class="dv">11</span>)</span>
502 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a>fluffy <span class="ot">&lt;-</span> <span class="fu">Cat</span>(<span class="at">name =</span> <span class="st">&quot;Fluffy&quot;</span>, <span class="at">age =</span> <span class="dv">5</span>)</span></code></pre></div>
503 <p>Method dispatch takes advantage of the hierarchy of parent classes:
504 if a method is not defined for a class, it will try the method for the
505 parent class, and so on until it finds a method or gives up with an
506 error. This inheritance is a powerful mechanism for sharing code across
507 classes.</p>
508 <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>describe <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;describe&quot;</span>, <span class="st">&quot;x&quot;</span>)</span>
509 <span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="fu">method</span>(describe, Pet) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
510 <span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="fu">paste0</span>(x<span class="sc">@</span>name, <span class="st">&quot; is &quot;</span>, x<span class="sc">@</span>age, <span class="st">&quot; years old&quot;</span>)</span>
511 <span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a>}</span>
512 <span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a><span class="fu">describe</span>(lola)</span>
513 <span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Lola is 11 years old&quot;</span></span>
514 <span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a><span class="fu">describe</span>(fluffy)</span>
515 <span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Fluffy is 5 years old&quot;</span></span>
516 <span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a></span>
517 <span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a><span class="fu">method</span>(describe, Dog) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
518 <span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a> <span class="fu">paste0</span>(x<span class="sc">@</span>name, <span class="st">&quot; is a &quot;</span>, x<span class="sc">@</span>age, <span class="st">&quot; year old dog&quot;</span>)</span>
519 <span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a>}</span>
520 <span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a><span class="fu">describe</span>(lola)</span>
521 <span id="cb16-14"><a href="#cb16-14" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Lola is a 11 year old dog&quot;</span></span>
522 <span id="cb16-15"><a href="#cb16-15" tabindex="-1"></a><span class="fu">describe</span>(fluffy)</span>
523 <span id="cb16-16"><a href="#cb16-16" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Fluffy is 5 years old&quot;</span></span></code></pre></div>
524 <p>You can define a fallback method for any S7 object by registering a
525 method for <code>S7_object</code>:</p>
526 <div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="fu">method</span>(describe, S7_object) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
527 <span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a> <span class="st">&quot;An S7 object&quot;</span></span>
528 <span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a>}</span>
529 <span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a></span>
530 <span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a>Cocktail <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Cocktail&quot;</span>,</span>
531 <span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span>
532 <span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a> <span class="at">ingredients =</span> class_character</span>
533 <span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a> )</span>
534 <span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a>)</span>
535 <span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a>martini <span class="ot">&lt;-</span> <span class="fu">Cocktail</span>(<span class="at">ingredients =</span> <span class="fu">c</span>(<span class="st">&quot;gin&quot;</span>, <span class="st">&quot;vermouth&quot;</span>))</span>
536 <span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="fu">describe</span>(martini)</span>
537 <span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a><span class="co">#&gt; [1] &quot;An S7 object&quot;</span></span></code></pre></div>
538 <p>Printing a generic will show you which methods are currently
539 defined:</p>
540 <div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>describe</span>
541 <span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="co">#&gt; &lt;S7_generic&gt; describe(x, ...) with 3 methods:</span></span>
542 <span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="co">#&gt; 1: method(describe, Dog)</span></span>
543 <span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="co">#&gt; 2: method(describe, S7_object)</span></span>
544 <span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="co">#&gt; 3: method(describe, Pet)</span></span></code></pre></div>
545 <p>And you can use <code>method()</code> to retrieve the implementation
546 of one of those methods:</p>
547 <div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="fu">method</span>(describe, Pet)</span>
548 <span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="co">#&gt; &lt;S7_method&gt; method(describe, Pet)</span></span>
549 <span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="co">#&gt; function (x) </span></span>
550 <span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="co">#&gt; {</span></span>
551 <span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co">#&gt; paste0(x@name, &quot; is &quot;, x@age, &quot; years old&quot;)</span></span>
552 <span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="co">#&gt; }</span></span>
553 <span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a><span class="co">#&gt; &lt;bytecode: 0x1335c0130&gt;</span></span></code></pre></div>
554 <p>Learn more about method dispatch in
555 <code>vignette(&quot;generics-methods&quot;)</code>.</p>
556 </div>
557
558
559
560 <!-- code folding -->
561
562
563 <!-- dynamically load mathjax for compatibility with self-contained -->
564 <script>
565 (function () {
566 var script = document.createElement("script");
567 script.type = "text/javascript";
568 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
569 document.getElementsByTagName("head")[0].appendChild(script);
570 })();
571 </script>
572
573 </body>
574 </html>