Debian Codegraph @

inst/doc/generics-methods.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>Generics and methods</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">Generics and methods</h1>
341
342
343
344 <p>This vignette dives into the details of S7 generics and method
345 dispatch, building on the basics discussed in
346 <code>vignette(&quot;S7&quot;)</code>. We’ll first introduce the concept of
347 generic-method compatibility, then discuss some of the finer details of
348 creating a generic with <code>new_generic()</code>. This vignette first
349 discusses generic-method compatibility, and you might want to customize
350 the body of the generic, and generics that live in suggested packages.
351 We’ll then pivot to talk more details of method dispatch including
352 <code>super()</code> and multiple dispatch.</p>
353 <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>
354 <div id="generic-method-compatibility" class="section level2">
355 <h2>Generic-method compatibility</h2>
356 <p>When you register a method, S7 checks that your method is compatible
357 with the generic.</p>
358 <p>The formal arguments of the generic and methods must agree. This
359 means that:</p>
360 <ul>
361 <li>Any arguments that the generic has, the method must have too. In
362 particular, the arguments of the method start with the arguments that
363 the generic dispatches on, and those arguments must not have default
364 arguments.</li>
365 <li>The method can contain arguments that the generic does not, as long
366 as the generic includes <code>…</code> in the argument list.</li>
367 </ul>
368 <div id="generic-with-dots-method-without-dots" class="section level3">
369 <h3>Generic with dots; method without dots</h3>
370 <p>The default generic includes <code>…</code> but generally the methods
371 should not. That ensures that misspelled arguments won’t be silently
372 swallowed by the method. This is an important difference from S3. Take a
373 very simple implementation of <code>mean()</code>:</p>
374 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>mean <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;mean&quot;</span>, <span class="st">&quot;x&quot;</span>)</span>
375 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">method</span>(mean, class_numeric) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) <span class="fu">sum</span>(x) <span class="sc">/</span> <span class="fu">length</span>(x)</span></code></pre></div>
376 <p>If we pass an additional argument in, we’ll get an error:</p>
377 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">mean</span>(<span class="dv">100</span>, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
378 <p>But we can still add additional arguments if we desired:</p>
379 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">method</span>(mean, class_numeric) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>) {</span>
380 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> <span class="cf">if</span> (na.rm) {</span>
381 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> x <span class="ot">&lt;-</span> x[<span class="sc">!</span><span class="fu">is.na</span>(x)]</span>
382 <span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a> }</span>
383 <span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a></span>
384 <span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a> <span class="fu">sum</span>(x) <span class="sc">/</span> <span class="fu">length</span>(x)</span>
385 <span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a>}</span>
386 <span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#&gt; Overwriting method mean(&lt;integer&gt;)</span></span>
387 <span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#&gt; Overwriting method mean(&lt;double&gt;)</span></span>
388 <span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">c</span>(<span class="dv">100</span>, <span class="cn">NA</span>), <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
389 <span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#&gt; [1] 100</span></span></code></pre></div>
390 <p>(We’ll come back to the case of requiring that all methods implement
391 a <code>na.rm = TRUE</code> argument shortly.)</p>
392 </div>
393 <div id="generic-and-method-with-dots" class="section level3">
394 <h3>Generic and method with dots</h3>
395 <p>There are cases where you do need to take <code>…</code> in a method,
396 which is particularly problematic if you need to re-call the generic
397 recursively. For example, imagine a simple print method like this:</p>
398 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>simple_print <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;simple_print&quot;</span>, <span class="st">&quot;x&quot;</span>)</span>
399 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="fu">method</span>(simple_print, class_double) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, <span class="at">digits =</span> <span class="dv">3</span>) {}</span>
400 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="fu">method</span>(simple_print, class_character) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, <span class="at">max_length =</span> <span class="dv">100</span>) {}</span></code></pre></div>
401 <p>What if you want to print a list?</p>
402 <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">method</span>(simple_print, class_list) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, ...) {</span>
403 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a> <span class="cf">for</span> (el <span class="cf">in</span> x) {</span>
404 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a> <span class="fu">simple_print</span>(el, ...)</span>
405 <span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a> }</span>
406 <span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a>}</span></code></pre></div>
407 <p>It’s fine as long as all the elements of the list are numbers, but as
408 soon as we add a character vector, we get an error:</p>
409 <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">simple_print</span>(<span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>), <span class="at">digits =</span> <span class="dv">3</span>)</span>
410 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">simple_print</span>(<span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="st">&quot;x&quot;</span>), <span class="at">digits =</span> <span class="dv">3</span>)</span></code></pre></div>
411 <p>To solve this situation, methods generally need to ignore arguments
412 that they haven’t been specifically designed to handle, i.e. they need
413 to use <code>…</code>:</p>
414 <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="fu">method</span>(simple_print, class_double) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, ..., <span class="at">digits =</span> <span class="dv">3</span>) {}</span>
415 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co">#&gt; Overwriting method simple_print(&lt;double&gt;)</span></span>
416 <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="fu">method</span>(simple_print, class_character) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, ..., <span class="at">max_length =</span> <span class="dv">100</span>) {}</span>
417 <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co">#&gt; Overwriting method simple_print(&lt;character&gt;)</span></span>
418 <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a></span>
419 <span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="fu">simple_print</span>(<span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="st">&quot;x&quot;</span>), <span class="at">digits =</span> <span class="dv">3</span>)</span></code></pre></div>
420 <p>In this case we really do want to silently ignore unknown arguments
421 because they might apply to other methods. There’s unfortunately no easy
422 way to avoid this problem without relying on fairly esoteric technology
423 (as done by <code>rlang::check_dots_used()</code>).</p>
424 <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">simple_print</span>(<span class="fu">list</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="st">&quot;x&quot;</span>), <span class="at">diggits =</span> <span class="dv">3</span>)</span></code></pre></div>
425 </div>
426 <div id="generic-and-method-without-dots" class="section level3">
427 <h3>Generic and method without dots</h3>
428 <p>Occasional it’s useful to create a generic without <code>…</code>
429 because such functions have a useful property: if a call succeeds for
430 one type of input, it will succeed for any type of input. To create such
431 a generic, you’ll need to use the third argument to
432 <code>new_generic()</code>: an optional function that powers the
433 generic. This function has one key property: it must call
434 <code>S7_dispatch()</code> to actually perform dispatch.</p>
435 <p>In general, this property is only needed for very low-level functions
436 with precisely defined semantics. A good example of such a function is
437 <code>length()</code>:</p>
438 <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>length <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;length&quot;</span>, <span class="st">&quot;x&quot;</span>, <span class="cf">function</span>(x) {</span>
439 <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a> <span class="fu">S7_dispatch</span>()</span>
440 <span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a>})</span></code></pre></div>
441 <p>Omitting <code>…</code> from the generic signature is a strong
442 restriction as it prevents methods from adding extra arguments. For this
443 reason, it should only be used in special situations.</p>
444 </div>
445 </div>
446 <div id="customizing-generics" class="section level2">
447 <h2>Customizing generics</h2>
448 <p>In most cases, you’ll supply the first two arguments to
449 <code>new_generic()</code> and allow it to automatically generate the
450 body of the generic:</p>
451 <div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>display <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;display&quot;</span>, <span class="st">&quot;x&quot;</span>)</span>
452 <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="fu">S7_data</span>(display)</span>
453 <span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#&gt; function (x, ...) </span></span>
454 <span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co">#&gt; S7::S7_dispatch()</span></span></code></pre></div>
455 <p>The most important part of the body is <code>S7_dispatch()</code>;
456 this function finds the method that matches the arguments used for
457 dispatch and calls it with the arguments supplied to the generic.</p>
458 <p>It can be useful to customize this body. The previous section showed
459 one case when you might want to supply the body yourself: dropping
460 <code>…</code> from the formals of the generic. There are three other
461 useful cases:</p>
462 <ul>
463 <li>To add required arguments.</li>
464 <li>To add optional arguments.</li>
465 <li>Perform some standard work.</li>
466 </ul>
467 <p>A custom <code>fun</code> must always include a call to
468 <code>S7_dispatch()</code>, which will usually be the last call.</p>
469 <div id="add-required-arguments" class="section level3">
470 <h3>Add required arguments</h3>
471 <p>To add required arguments that aren’t dispatched upon, you just need
472 to add additional arguments that lack default values:</p>
473 <div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>foo <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;foo&quot;</span>, <span class="st">&quot;x&quot;</span>, <span class="cf">function</span>(x, y, ...) {</span>
474 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a> <span class="fu">S7_dispatch</span>()</span>
475 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a>})</span></code></pre></div>
476 <p>Now all methods will need to provide that <code>y</code> argument. If
477 not, you’ll get a warning:</p>
478 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="fu">method</span>(foo, class_integer) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, ...) {</span>
479 <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="dv">10</span></span>
480 <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a>}</span>
481 <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="co">#&gt; Warning: foo(&lt;integer&gt;) doesn&#39;t have argument `y`</span></span></code></pre></div>
482 <p>This is a warning, not an error, because the generic might be defined
483 in a different package and is in the process of changing interfaces.
484 You’ll always want to address this warning when you see it.</p>
485 </div>
486 <div id="add-optional-arguments" class="section level3">
487 <h3>Add optional arguments</h3>
488 <p>Adding an optional argument is similar, but it should generally come
489 after <code>…</code>. This ensures that the user must supply the full
490 name of the argument when calling the function, which makes it easier to
491 extend your function in the future.</p>
492 <div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>mean <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;mean&quot;</span>, <span class="st">&quot;x&quot;</span>, <span class="cf">function</span>(x, ..., <span class="at">na.rm =</span> <span class="cn">TRUE</span>) {</span>
493 <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a> <span class="fu">S7_dispatch</span>()</span>
494 <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a>})</span>
495 <span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="fu">method</span>(mean, class_integer) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>) {</span>
496 <span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a> <span class="cf">if</span> (na.rm) {</span>
497 <span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a> x <span class="ot">&lt;-</span> x[<span class="sc">!</span><span class="fu">is.na</span>(x)]</span>
498 <span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a> }</span>
499 <span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a> <span class="fu">sum</span>(x) <span class="sc">/</span> <span class="fu">length</span>(x)</span>
500 <span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a>}</span></code></pre></div>
501 <p>Forgetting the argument or using a different default value will again
502 generate a warning.</p>
503 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="fu">method</span>(mean, class_double) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, <span class="at">na.rm =</span> <span class="cn">FALSE</span>) {}</span>
504 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="co">#&gt; Warning: In mean(&lt;double&gt;), default value of `na.rm` is not the same as the generic</span></span>
505 <span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="co">#&gt; - Generic: TRUE</span></span>
506 <span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a><span class="co">#&gt; - Method: FALSE</span></span>
507 <span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="fu">method</span>(mean, class_logical) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {}</span>
508 <span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="co">#&gt; Warning: mean(&lt;logical&gt;) doesn&#39;t have argument `na.rm`</span></span></code></pre></div>
509 </div>
510 <div id="do-some-work" class="section level3">
511 <h3>Do some work</h3>
512 <p>If your generic has additional arguments, you might want to do some
513 additional work to verify that they’re of the expected type. For
514 example, our <code>mean()</code> function could verify that
515 <code>na.rm</code> was correctly specified:</p>
516 <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>mean <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;mean&quot;</span>, <span class="st">&quot;x&quot;</span>, <span class="cf">function</span>(x, ..., <span class="at">na.rm =</span> <span class="cn">TRUE</span>) {</span>
517 <span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">identical</span>(na.rm, <span class="cn">TRUE</span>) <span class="sc">&amp;&amp;</span> <span class="sc">!</span><span class="fu">identical</span>(<span class="at">na.rm =</span> <span class="cn">FALSE</span>)) {</span>
518 <span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">&quot;`na.rm` must be either TRUE or FALSE&quot;</span>)</span>
519 <span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> }</span>
520 <span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> <span class="fu">S7_dispatch</span>()</span>
521 <span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a>})</span></code></pre></div>
522 <p>The only downside to performing error checking is that you constraint
523 the interface for all methods; if for some reason a method found it
524 useful to allow <code>na.rm</code> to be a number or a string, it would
525 have to provide an alternative argument.</p>
526 </div>
527 </div>
528 <div id="super" class="section level2">
529 <h2><code>super()</code></h2>
530 <p>Sometimes it’s useful to define a method for a class that relies on
531 the implementation for its superclass. A good example of this is
532 computing the mean of a date — since dates represent the number of days
533 since 1970-01-01, computing the mean is just a matter of computing the
534 mean of the underlying numeric vector and converting it back to a
535 date.</p>
536 <p>To demonstrate this idea, I’ll first define a mean generic with a
537 method for numbers:</p>
538 <div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a>mean <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;mean&quot;</span>, <span class="st">&quot;x&quot;</span>)</span>
539 <span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="fu">method</span>(mean, class_numeric) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
540 <span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> <span class="fu">sum</span>(x) <span class="sc">/</span> <span class="fu">length</span>(x)</span>
541 <span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a>}</span>
542 <span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a><span class="fu">mean</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>)</span>
543 <span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a><span class="co">#&gt; [1] 5.5</span></span></code></pre></div>
544 <p>And a Date class:</p>
545 <div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>date <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;date&quot;</span>, <span class="at">parent =</span> class_double)</span>
546 <span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a><span class="co"># Cheat by using the existing base .Date class</span></span>
547 <span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="fu">method</span>(print, date) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) <span class="fu">print</span>(<span class="fu">.Date</span>(x))</span>
548 <span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a><span class="fu">date</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">100</span>))</span>
549 <span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="co">#&gt; [1] &quot;1970-01-02&quot; &quot;1970-01-11&quot; &quot;1970-04-11&quot;</span></span></code></pre></div>
550 <p>Now to compute a mean we write:</p>
551 <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>(mean, date) <span class="ot">&lt;-</span> <span class="cf">function</span>(x) {</span>
552 <span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> <span class="fu">date</span>(<span class="fu">mean</span>(<span class="fu">super</span>(x, <span class="at">to =</span> class_double)))</span>
553 <span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a>}</span>
554 <span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="fu">mean</span>(<span class="fu">date</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">100</span>)))</span>
555 <span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co">#&gt; [1] &quot;1970-02-07&quot;</span></span></code></pre></div>
556 <p>Let’s unpack this method from the inside out:</p>
557 <ol style="list-style-type: decimal">
558 <li>First we call <code>super(x, to = class_double)</code> — this will
559 make the call to next generic treat <code>x</code> like it’s a double,
560 rather than a date.</li>
561 <li>Then we call <code>mean()</code> which because of
562 <code>super()</code> will call the <code>mean()</code> method we defined
563 above.</li>
564 <li>Finally, we take the number returned by mean and convert it back to
565 a date.</li>
566 </ol>
567 <p>If you’re very familiar with S3 or S4 you might recognize that
568 <code>super()</code> fills a similar role to <code>NextMethod()</code>
569 or <code>callNextMethod()</code>. However, it’s much more explicit: you
570 need to supply the name of the parent class, the generic to use, and all
571 the arguments to the generic. This explicitness makes the code easier to
572 understand and will eventually enable certain performance optimizations
573 that would otherwise be very difficult.</p>
574 </div>
575 <div id="multiple-dispatch" class="section level2">
576 <h2>Multiple dispatch</h2>
577 <p>So far we have focused primarily on single dispatch, i.e. generics
578 where <code>dispatch_args</code> is a single string. It is also possible
579 to supply a length 2 (or more!) vector <code>dispatch_args</code> to
580 create a generic that performs multiple dispatch, i.e. it uses the
581 classes of more than one object to find the appropriate method.</p>
582 <p>Multiple dispatch is a feature primarily of S4, although S3 includes
583 some limited special cases for arithmetic operators. Multiple dispatch
584 is heavily used in S4; we don’t expect it to be heavily used in S7, but
585 it is occasionally useful.</p>
586 <div id="a-simple-example" class="section level3">
587 <h3>A simple example</h3>
588 <p>Let’s take our speak example from <code>vignette(&quot;S7&quot;)</code> and
589 extend it to teach our pets how to speak multiple languages:</p>
590 <div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>Pet <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Pet&quot;</span>)</span>
591 <span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a>Dog <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Dog&quot;</span>, Pet)</span>
592 <span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a>Cat <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Cat&quot;</span>, Pet)</span>
593 <span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a></span>
594 <span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a>Language <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;Language&quot;</span>)</span>
595 <span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a>English <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;English&quot;</span>, Language)</span>
596 <span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a>French <span class="ot">&lt;-</span> <span class="fu">new_class</span>(<span class="st">&quot;French&quot;</span>, Language)</span>
597 <span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a></span>
598 <span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a>speak <span class="ot">&lt;-</span> <span class="fu">new_generic</span>(<span class="st">&quot;speak&quot;</span>, <span class="fu">c</span>(<span class="st">&quot;x&quot;</span>, <span class="st">&quot;y&quot;</span>))</span>
599 <span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a><span class="fu">method</span>(speak, <span class="fu">list</span>(Dog, English)) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, y) <span class="st">&quot;Woof&quot;</span></span>
600 <span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="fu">method</span>(speak, <span class="fu">list</span>(Cat, English)) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, y) <span class="st">&quot;Meow&quot;</span></span>
601 <span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a><span class="fu">method</span>(speak, <span class="fu">list</span>(Dog, French)) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, y) <span class="st">&quot;Ouaf Ouaf&quot;</span></span>
602 <span id="cb20-13"><a href="#cb20-13" tabindex="-1"></a><span class="fu">method</span>(speak, <span class="fu">list</span>(Cat, French)) <span class="ot">&lt;-</span> <span class="cf">function</span>(x, y) <span class="st">&quot;Miaou&quot;</span></span>
603 <span id="cb20-14"><a href="#cb20-14" tabindex="-1"></a></span>
604 <span id="cb20-15"><a href="#cb20-15" tabindex="-1"></a><span class="fu">speak</span>(<span class="fu">Cat</span>(), <span class="fu">English</span>())</span>
605 <span id="cb20-16"><a href="#cb20-16" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Meow&quot;</span></span>
606 <span id="cb20-17"><a href="#cb20-17" tabindex="-1"></a><span class="fu">speak</span>(<span class="fu">Dog</span>(), <span class="fu">French</span>())</span>
607 <span id="cb20-18"><a href="#cb20-18" tabindex="-1"></a><span class="co">#&gt; [1] &quot;Ouaf Ouaf&quot;</span></span>
608 <span id="cb20-19"><a href="#cb20-19" tabindex="-1"></a></span>
609 <span id="cb20-20"><a href="#cb20-20" tabindex="-1"></a><span class="co"># This example was originally inspired by blog.klipse.tech/javascript/2021/10/03/multimethod.html</span></span>
610 <span id="cb20-21"><a href="#cb20-21" tabindex="-1"></a><span class="co"># which has unfortunately since disappeared.</span></span></code></pre></div>
611 </div>
612 <div id="special-classes" class="section level3">
613 <h3>Special “classes”</h3>
614 <p>There are two special classes that become particularly useful with
615 multiple dispatch:</p>
616 <ul>
617 <li><code>class_any()</code> will match any class</li>
618 <li><code>class_missing()</code> will match a missing argument (i.e. not
619 <code>NA</code>, but an argument that was not supplied)</li>
620 </ul>
621 </div>
622 </div>
623
624
625
626 <!-- code folding -->
627
628
629 <!-- dynamically load mathjax for compatibility with self-contained -->
630 <script>
631 (function () {
632 var script = document.createElement("script");
633 script.type = "text/javascript";
634 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
635 document.getElementsByTagName("head")[0].appendChild(script);
636 })();
637 </script>
638
639 </body>
640 </html>