forked from moodymudskipper/typed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
465 lines (436 loc) · 33.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Support Types for Variables, Arguments, and Return Values • typed</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="bootstrap-toc.css">
<script src="bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="pkgdown.css" rel="stylesheet">
<script src="pkgdown.js"></script><meta property="og:title" content="Support Types for Variables, Arguments, and Return Values">
<meta property="og:description" content="A type system for R. It supports setting variable types in a script or the body of a function, so variables can't be assigned illegal values. Moreover it supports setting argument and return types for functions.">
<meta property="og:image" content="/logo.png">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container template-home">
<header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="index.html">typed</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.0.1.9000</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">
<span class="fas fa fas fa-home fa-lg"></span>
</a>
</li>
<li>
<a href="reference/index.html">Reference</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/moodymudskipper/typed/">
<span class="fab fa fab fa-github fa-lg"></span>
</a>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container -->
</div>
<!--/.navbar -->
</header><div class="row">
<div class="contents col-md-9">
<div id="typed-" class="section level1">
<div class="page-header"><h1 class="hasAnchor">
<a href="#typed-" class="anchor"></a>typed <img src="reference/figures/logo.png" align="right" height="139">
</h1></div>
<p><em>{typed}</em> implements a type system for R, it has 3 main features:</p>
<ul>
<li>set variable types in a script or the body of a function, so they can’t be assigned illegal values</li>
<li>set argument types in a function definition</li>
<li>set return type of a function</li>
</ul>
<p>The user can define their own types, or leverage assertions from other packages.</p>
<p>Under the hood variable types use active bindings, so once a variable is restricted by an assertion, it cannot be modified in a way that would not satisfy it.</p>
<div id="installation" class="section level2">
<h2 class="hasAnchor">
<a href="#installation" class="anchor"></a>Installation</h2>
<p>Install CRAN version with:</p>
<div class="sourceCode" id="cb1"><pre class="r"><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span>(<span class="st">"typed"</span>)</pre></div>
<p>or development version with :</p>
<div class="sourceCode" id="cb2"><pre class="r"><span class="kw pkg">remotes</span><span class="kw ns">::</span><span class="fu"><a href="https://remotes.r-lib.org/reference/install_github.html">install_github</a></span>(<span class="st">"moodymudskipper/typed"</span>)</pre></div>
<p>And attach with :</p>
<div class="sourceCode" id="cb3"><pre class="r"><span class="co"># masking warning about overriding `?`</span>
<span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">typed</span>, <span class="kw">warn.conflicts</span> <span class="kw">=</span> <span class="fl">FALSE</span>)</pre></div>
</div>
<div id="set-variable-type" class="section level2">
<h2 class="hasAnchor">
<a href="#set-variable-type" class="anchor"></a>Set variable type</h2>
<div id="question-mark-notation-and-declare" class="section level3">
<h3 class="hasAnchor">
<a href="#question-mark-notation-and-declare" class="anchor"></a>Question mark notation and <code>declare</code>
</h3>
<p>Here are examples on how we would set types</p>
<div class="sourceCode" id="cb4"><pre class="r"><span class="fu"><a href="reference/assertion_factories.html">Character</a></span>() ? <span class="no">x</span> <span class="co"># restrict x to "character" type</span>
<span class="no">x</span> <span class="kw"><-</span> <span class="st">"a"</span>
<span class="no">x</span>
<span class="co">#> [1] "a"</span>
<span class="fu"><a href="reference/assertion_factories.html">Integer</a></span>(<span class="fl">3</span>) ? <span class="no">y</span> <span class="kw"><-</span> <span class="fl">1</span>:<span class="fl">3</span> <span class="co"># restrict y to "integer" type of length 3</span>
<span class="no">y</span>
<span class="co">#> [1] 1 2 3</span></pre></div>
<p>We cannot assign values of the wrong type to <code>x</code> and <code>y</code> anymore.</p>
<div class="sourceCode" id="cb5"><pre class="r"><span class="no">x</span> <span class="kw"><-</span> <span class="fl">2</span>
<span class="co">#> Error: type mismatch</span>
<span class="co">#> `typeof(value)`: "double" </span>
<span class="co">#> `expected`: "character"</span>
<span class="no">y</span> <span class="kw"><-</span> <span class="fl">4</span>:<span class="fl">5</span>
<span class="co">#> Error: length mismatch</span>
<span class="co">#> `length(value)`: 2</span>
<span class="co">#> `expected`: 3</span></pre></div>
<p>But the right type will work.</p>
<div class="sourceCode" id="cb6"><pre class="r"><span class="no">x</span> <span class="kw"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"b"</span>, <span class="st">"c"</span>)
<span class="no">y</span> <span class="kw"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">1L</span>, <span class="fl">10L</span>, <span class="fl">100L</span>)</pre></div>
<p><code>declare</code> is a strict equivalent, slightly more efficient, which looks like <code><a href="https://rdrr.io/r/base/assign.html">base::assign</a></code>.</p>
<div class="sourceCode" id="cb7"><pre class="r"><span class="fu"><a href="reference/declare.html">declare</a></span>(<span class="st">"x"</span>, <span class="fu"><a href="reference/assertion_factories.html">Character</a></span>())
<span class="no">x</span> <span class="kw"><-</span> <span class="st">"a"</span>
<span class="no">x</span>
<span class="co">#> [1] "a"</span>
<span class="fu"><a href="reference/declare.html">declare</a></span>(<span class="st">"y"</span>, <span class="fu"><a href="reference/assertion_factories.html">Integer</a></span>(<span class="fl">3</span>), <span class="fl">1</span>:<span class="fl">3</span>)
<span class="no">y</span>
<span class="co">#> [1] 1 2 3</span></pre></div>
</div>
<div id="assertion-factories-and-assertions" class="section level3">
<h3 class="hasAnchor">
<a href="#assertion-factories-and-assertions" class="anchor"></a>Assertion factories and assertions</h3>
<p><code>Integer</code> and <code>Character</code> are function factories (functions that return functions), thus <code><a href="reference/assertion_factories.html">Integer(3)</a></code> and <code><a href="reference/assertion_factories.html">Character()</a></code> are functions.</p>
<p>The latter functions operate checks on a value and in case of success return this value, generally unmodified. For instance :</p>
<div class="sourceCode" id="cb8"><pre class="r"><span class="fu"><a href="reference/assertion_factories.html">Integer</a></span>(<span class="fl">3</span>)(<span class="fl">1</span>:<span class="fl">2</span>)
<span class="co">#> Error: length mismatch</span>
<span class="co">#> `length(value)`: 2</span>
<span class="co">#> `expected`: 3</span>
<span class="fu"><a href="reference/assertion_factories.html">Character</a></span>()(<span class="fl">3</span>)
<span class="co">#> Error: type mismatch</span>
<span class="co">#> `typeof(value)`: "double" </span>
<span class="co">#> `expected`: "character"</span></pre></div>
<p>We call <code><a href="reference/assertion_factories.html">Integer(3)</a></code> and <code><a href="reference/assertion_factories.html">Character()</a></code> assertions, and we call <code>Integer</code> and <code>Character</code> assertion factories.</p>
<p>The package contains many assertion factories (see <code>?assertion_factories</code>), the main ones are:</p>
<ul>
<li>
<code>Any</code> (No default restriction)</li>
<li><code>Logical</code></li>
<li><code>Integer</code></li>
<li><code>Double</code></li>
<li><code>Character</code></li>
<li><code>List</code></li>
<li><code>Environment</code></li>
<li><code>Factor</code></li>
<li><code>Matrix</code></li>
<li><code>Data.frame</code></li>
<li><code>Date</code></li>
<li>
<code>Time</code> (POSIXct)</li>
</ul>
</div>
<div id="custom-assertions" class="section level3">
<h3 class="hasAnchor">
<a href="#custom-assertions" class="anchor"></a>Custom assertions</h3>
<p>As we’ve seen with <code><a href="reference/assertion_factories.html">Integer(3)</a></code>, passing arguments to a assertion factory restricts the type.</p>
<p>For instance <code>Integer</code> has arguments <code>length</code> <code>null_ok</code> and <code>...</code>, we already used <code>length</code>, <code>null_ok</code> is convenient to allow a default <code>NULL</code> value in addition to the <code>"integer"</code> type. In the dots we can use arguments named as functions and with the value of the expected result.</p>
<div class="sourceCode" id="cb9"><pre class="r"><span class="fu"><a href="reference/assertion_factories.html">Integer</a></span>(<span class="kw">anyNA</span> <span class="kw">=</span> <span class="fl">FALSE</span>) ? <span class="no">x</span> <span class="kw"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">1L</span>, <span class="fl">2L</span>, <span class="fl">NA</span>)
<span class="co">#> Error: `anyNA` mismatch</span>
<span class="co">#> `anyNA(value)`: TRUE </span>
<span class="co">#> `expected`: FALSE</span></pre></div>
<p>Useful arguments might be for instance, <code>anyDuplicated = 0L</code>, <code>names = NULL</code>, <code>attributes = NULL</code>… Any available function can be used.</p>
<p>That makes assertion factories very flexible! If it is still not flexible enough, one can provide conditions using formulas in the <code>...</code>. Be careful to skip all named arguments by adding comas, or name the formula arguments <code>...</code>.</p>
<div class="sourceCode" id="cb10"><pre class="r"><span class="no">fruit</span> <span class="kw"><-</span> <span class="fu"><a href="reference/assertion_factories.html">Character</a></span>(<span class="fl">1</span>, <span class="kw">...</span> <span class="kw">=</span> <span class="st">"`value` is not a fruit!"</span> ~ <span class="no">.</span> <span class="kw">%in%</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"apple"</span>, <span class="st">"pear"</span>, <span class="st">"cherry"</span>))
<span class="no">fruit</span> ? <span class="no">x</span> <span class="kw"><-</span> <span class="st">"potatoe"</span>
<span class="co">#> Error: `value` is not a fruit!</span>
<span class="co">#> `value %in% c("apple", "pear", "cherry")`: FALSE</span>
<span class="co">#> `expected`: TRUE</span></pre></div>
<p>The arguments can differ between assertion factories, for instance <code>Data.frame</code> has <code>nrow</code>, <code>ncol</code>, <code>each</code>, <code>null_ok</code> and <code>...</code></p>
<div class="sourceCode" id="cb11"><pre class="r"><span class="fu"><a href="reference/assertion_factories.html">Data.frame</a></span>() ? <span class="no">x</span> <span class="kw"><-</span> <span class="no">iris</span>
<span class="fu"><a href="reference/assertion_factories.html">Data.frame</a></span>(<span class="kw">ncol</span> <span class="kw">=</span> <span class="fl">2</span>) ? <span class="no">x</span> <span class="kw"><-</span> <span class="no">iris</span>
<span class="co">#> Error: Column number mismatch</span>
<span class="co">#> `ncol(value)`: 5</span>
<span class="co">#> `expected`: 2</span>
<span class="fu"><a href="reference/assertion_factories.html">Data.frame</a></span>(<span class="kw">each</span> <span class="kw">=</span> <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>()) ? <span class="no">x</span> <span class="kw"><-</span> <span class="no">iris</span>
<span class="co">#> Error: column 5 ("Species") type mismatch</span>
<span class="co">#> `typeof(value)`: "integer"</span>
<span class="co">#> `expected`: "double"</span></pre></div>
</div>
<div id="leverage-assertions-from-other-packages-build-your-own-assertion-factories" class="section level3">
<h3 class="hasAnchor">
<a href="#leverage-assertions-from-other-packages-build-your-own-assertion-factories" class="anchor"></a>Leverage assertions from other packages, build your own assertion factories</h3>
<p>Some great packages provide assertions, and they can be used with <code>typed</code> provided that they take the object as a first input and return the object if no failure. Richie Cotton’s <em>{assertive}</em> and Michel Lang’s <em>{checkmate}</em> both qualify.</p>
<div class="sourceCode" id="cb12"><pre class="r"><span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">assertive</span>)
<span class="no">assert_is_monotonic_increasing</span> ? <span class="no">z</span>
<span class="no">z</span> <span class="kw"><-</span> <span class="fl">3</span>:<span class="fl">1</span>
<span class="co">#> Error: is_monotonic_increasing : The values of assigned_value are not monotonic increasing.</span>
<span class="co">#> Position ValueBefore ValueAfter</span>
<span class="co">#> 1 1/2 3 2</span>
<span class="co">#> 2 2/3 2 1</span></pre></div>
<p>If we want to use more than the first argument, we should create an assertion factory :</p>
<div class="sourceCode" id="cb13"><pre class="r"><span class="no">Monotonic_incr</span> <span class="kw"><-</span> <span class="fu"><a href="reference/as_assertion_factory.html">as_assertion_factory</a></span>(<span class="no">assert_is_monotonic_increasing</span>)
<span class="fu">Monotonic_incr</span>(<span class="kw">strictly</span> <span class="kw">=</span> <span class="fl">TRUE</span>) ? <span class="no">z</span>
<span class="no">z</span> <span class="kw"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">1</span>, <span class="fl">1</span>, <span class="fl">2</span>)
<span class="co">#> Error: is_monotonic_increasing : The values of value are not strictly monotonic increasing.</span>
<span class="co">#> Position ValueBefore ValueAfter</span>
<span class="co">#> 1 1/2 1 1</span></pre></div>
<p><code>as_assertion_factory</code> can be used to create your own assertion factories from scratch too, in fact <a href="https://github.com/moodymudskipper/typed/blob/master/R/06_native_types.R">it’s used to build the native assertion factories of this package</a> .</p>
</div>
<div id="constants" class="section level3">
<h3 class="hasAnchor">
<a href="#constants" class="anchor"></a>Constants</h3>
<p>To define a constant, we just surround the variable by parentheses (think of them as a protection)</p>
<div class="sourceCode" id="cb14"><pre class="r"><span class="fu"><a href="reference/assertion_factories.html">Double</a></span>() ? (<span class="no">x</span>) <span class="kw"><-</span> <span class="fl">1</span>
<span class="no">x</span> <span class="kw"><-</span> <span class="fl">2</span>
<span class="co">#> Error: Can't assign to a constant</span>
? (<span class="no">y</span>) <span class="kw"><-</span> <span class="fl">1</span>
<span class="no">y</span> <span class="kw"><-</span> <span class="fl">2</span>
<span class="co">#> Error: Can't assign to a constant</span></pre></div>
</div>
</div>
<div id="set-argument-type" class="section level2">
<h2 class="hasAnchor">
<a href="#set-argument-type" class="anchor"></a>Set argument type</h2>
<p>We can set argument types this way :</p>
<div class="sourceCode" id="cb15"><pre class="r"><span class="no">add</span> <span class="kw"><-</span> ? <span class="kw">function</span> (<span class="no">x</span><span class="kw">=</span> ? <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>(), <span class="no">y</span><span class="kw">=</span> <span class="fl">1</span> ? <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>()) {
<span class="no">x</span> + <span class="no">y</span>
}</pre></div>
<p>Note that we started the definition with a <code>?</code>, and that we gave a default to <code>y</code>, but not <code>x</code>. Note also the <code>=</code> sign next to <code>x</code>, necessary even when we have no default value. If you forget it you’ll have an error “unexpected <code>?</code> in …”.</p>
<p>This created the following function, by adding checks at the top of the body</p>
<div class="sourceCode" id="cb16"><pre class="r"><span class="no">add</span>
<span class="co">#> # typed function</span>
<span class="co">#> function (x, y = 1) </span>
<span class="co">#> {</span>
<span class="co">#> check_arg(x, Double())</span>
<span class="co">#> check_arg(y, Double())</span>
<span class="co">#> x + y</span>
<span class="co">#> }</span>
<span class="co">#> # Arg types:</span>
<span class="co">#> # x: Double()</span>
<span class="co">#> # y: Double()</span></pre></div>
<p>Let’s test it by providing a right and wrong type.</p>
<div class="sourceCode" id="cb17"><pre class="r"><span class="fu">add</span>(<span class="fl">2</span>, <span class="fl">3</span>)
<span class="co">#> [1] 5</span>
<span class="fu">add</span>(<span class="fl">2</span>, <span class="fl">3L</span>)
<span class="co">#> Error: In `add(2, 3L)` at `check_arg(y, Double())`:</span>
<span class="co">#> wrong argument to function, type mismatch</span>
<span class="co">#> `typeof(value)`: "integer"</span>
<span class="co">#> `expected`: "double"</span></pre></div>
<p>If we want to restrict <code>x</code> and <code>y</code> to the type “integer” in the rest of the body of the function we can use the <code>?+</code> notation :</p>
<div class="sourceCode" id="cb18"><pre class="r"><span class="no">add</span> <span class="kw"><-</span> ? <span class="kw">function</span> (<span class="no">x</span><span class="kw">=</span> ?+ <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>(), <span class="no">y</span><span class="kw">=</span> <span class="fl">1</span> ?+ <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>()) {
<span class="no">x</span> + <span class="no">y</span>
}
<span class="no">add</span>
<span class="co">#> # typed function</span>
<span class="co">#> function (x, y = 1) </span>
<span class="co">#> {</span>
<span class="co">#> check_arg(x, Double(), .bind = TRUE)</span>
<span class="co">#> check_arg(y, Double(), .bind = TRUE)</span>
<span class="co">#> x + y</span>
<span class="co">#> }</span>
<span class="co">#> # Arg types:</span>
<span class="co">#> # x: Double()</span>
<span class="co">#> # y: Double()</span></pre></div>
<p>We see that it is translated into a <code>check_arg</code> call containing a <code>.bind = TRUE</code> argument.</p>
<p>I we want to restrict the quoted expression rather than the value of an argument, we can use <code>?~</code> :</p>
<div class="sourceCode" id="cb19"><pre class="r"><span class="no">identity_sym_only</span> <span class="kw"><-</span> ? <span class="kw">function</span> (<span class="no">x</span><span class="kw">=</span> ?~ <span class="fu"><a href="reference/assertion_factories.html">Symbol</a></span>()) {
<span class="no">x</span>
}
<span class="no">a</span> <span class="kw"><-</span> <span class="fl">1</span>
<span class="fu">identity_sym_only</span>(<span class="no">a</span>)
<span class="co">#> [1] 1</span>
<span class="fu">identity_sym_only</span>(<span class="no">a</span> + <span class="no">a</span>)
<span class="co">#> Error: In `identity_sym_only(a + a)` at `check_arg(substitute(x), Symbol())`:</span>
<span class="co">#> wrong argument to function, type mismatch</span>
<span class="co">#> `typeof(value)`: "language"</span>
<span class="co">#> `expected`: "symbol"</span>
<span class="no">identity_sym_only</span>
<span class="co">#> # typed function</span>
<span class="co">#> function (x) </span>
<span class="co">#> {</span>
<span class="co">#> check_arg(substitute(x), Symbol())</span>
<span class="co">#> x</span>
<span class="co">#> }</span>
<span class="co">#> <bytecode: 0x000000001cb34218></span>
<span class="co">#> # Arg types:</span>
<span class="co">#> # x: ~Symbol()</span></pre></div>
<p>We see that it is translated into a <code>check_arg</code> call containing a call to <code>substitute</code> as the first argument. The <code>~</code> is kept in the attributes of the function.</p>
<p>We can also check the <code>...</code>, for instance use <code>function(... = ? Integer())</code> to check that only integers are passed to the dots, and use <code>function(... = ?~ Symbol())</code> to check that all quoted values passed to <code>...</code> are symbols.</p>
<p>The special assertion factory <code>Dots</code> can also be used, in that case the checks will apply to <code><a href="https://rdrr.io/r/base/list.html">list(...)</a></code> rather than to each element individually, for instance <code>function(... = ? Dots(2))</code> makes sure the dots were fed 2 values. In a similar fashion <code>function(... = ?~ Dots(2))</code> can be used to apply checks to the list of quoted argument passed to <code>...</code>.</p>
</div>
<div id="set-function-return-type" class="section level2">
<h2 class="hasAnchor">
<a href="#set-function-return-type" class="anchor"></a>Set function return type</h2>
<p>To set a return type we use <code>?</code> before the function definition as in the previous section, but we type an assertion on the left hand side.</p>
<div class="sourceCode" id="cb20"><pre class="r"><span class="no">add_or_subtract</span> <span class="kw"><-</span> <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>() ? <span class="kw">function</span> (<span class="no">x</span>, <span class="no">y</span>, <span class="no">subtract</span> <span class="kw">=</span> <span class="fl">FALSE</span>) {
<span class="kw">if</span>(<span class="no">subtract</span>) <span class="fu"><a href="https://rdrr.io/r/base/function.html">return</a></span>(<span class="no">x</span> - <span class="no">y</span>)
<span class="no">x</span> + <span class="no">y</span>
}
<span class="no">add_or_subtract</span>
<span class="co">#> # typed function</span>
<span class="co">#> function (x, y, subtract = FALSE) </span>
<span class="co">#> {</span>
<span class="co">#> if (subtract) </span>
<span class="co">#> return(check_output(x - y, Double()))</span>
<span class="co">#> check_output(x + y, Double())</span>
<span class="co">#> }</span>
<span class="co">#> # Return type: Double()</span></pre></div>
<p>We see that the returned values have been wrapped inside <code>check_output</code> calls.</p>
</div>
<div id="putting-it-all-together-write-packages-using-typed" class="section level2">
<h2 class="hasAnchor">
<a href="#putting-it-all-together-write-packages-using-typed" class="anchor"></a>Putting it all together, write packages using {typed}</h2>
<p>Let’s define our function for our package and document it with <em>{roxygen2}</em>. It is documented as usual,except that you’ll need to make sure to add the <code>@name</code> tag.</p>
<p>We declare types for the return value, for all arguments, and we declare a string <code>msg</code>.</p>
<div class="sourceCode" id="cb21"><pre class="r"><span class="co">#' add_or_subtract</span>
<span class="co">#'</span>
<span class="co">#' @param x double of length 1</span>
<span class="co">#' @param y double of length 1</span>
<span class="co">#' @param subtract whether to subtract instead of adding</span>
<span class="co">#' @export</span>
<span class="co">#' @name add_or_subtract</span>
<span class="no">add_or_subtract</span> <span class="kw"><-</span>
<span class="fu"><a href="reference/assertion_factories.html">Double</a></span>(<span class="fl">1</span>) ? <span class="kw">function</span> (
<span class="no">x</span><span class="kw">=</span> ? <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>(<span class="fl">1</span>),
<span class="no">y</span><span class="kw">=</span> ? <span class="fu"><a href="reference/assertion_factories.html">Double</a></span>(<span class="fl">1</span>),
<span class="no">subtract</span> <span class="kw">=</span> <span class="fl">FALSE</span> ? <span class="fu"><a href="reference/assertion_factories.html">Logical</a></span>(<span class="fl">1</span>, <span class="kw">anyNA</span> <span class="kw">=</span> <span class="fl">FALSE</span>)
) {
<span class="fu"><a href="reference/assertion_factories.html">Character</a></span>(<span class="fl">1</span>) ? <span class="no">msg</span>
<span class="kw">if</span>(<span class="no">subtract</span>) {
<span class="no">msg</span> <span class="kw"><-</span> <span class="st">"subtracting"</span>
<span class="fu"><a href="https://rdrr.io/r/base/message.html">message</a></span>(<span class="no">msg</span>)
<span class="fu"><a href="https://rdrr.io/r/base/function.html">return</a></span>(<span class="no">x</span> - <span class="no">y</span>)
}
<span class="no">msg</span> <span class="kw"><-</span> <span class="st">"adding"</span>
<span class="fu"><a href="https://rdrr.io/r/base/message.html">message</a></span>(<span class="no">msg</span>)
<span class="no">x</span> + <span class="no">y</span>
}</pre></div>
<p>The created function will be the following, we see that <code>Character(1) ? msg</code> was changed into a <code>declare</code> call too, this is both for efficiency and readability. Unfamiliar users might be intimidated by <code>?</code> and calls to <code>?</code> don’t print nicely.</p>
<div class="sourceCode" id="cb22"><pre class="r"><span class="no">add_or_subtract</span>
<span class="co">#> # typed function</span>
<span class="co">#> function (x, y, subtract = FALSE) </span>
<span class="co">#> {</span>
<span class="co">#> check_arg(x, Double(1))</span>
<span class="co">#> check_arg(y, Double(1))</span>
<span class="co">#> check_arg(subtract, Logical(1, anyNA = FALSE))</span>
<span class="co">#> declare("msg", Character(1))</span>
<span class="co">#> if (subtract) {</span>
<span class="co">#> msg <- "subtracting"</span>
<span class="co">#> message(msg)</span>
<span class="co">#> return(check_output(x - y, Double(1)))</span>
<span class="co">#> }</span>
<span class="co">#> msg <- "adding"</span>
<span class="co">#> message(msg)</span>
<span class="co">#> check_output(x + y, Double(1))</span>
<span class="co">#> }</span>
<span class="co">#> # Return type: Double(1)</span>
<span class="co">#> # Arg types:</span>
<span class="co">#> # x: Double(1)</span>
<span class="co">#> # y: Double(1)</span>
<span class="co">#> # subtract: Logical(1, anyNA = FALSE)</span></pre></div>
<p>Note that your package would import <em>{typed}</em> but <code>?</code> won’t be exposed to the user, they will see it in the code but will be able to use <code>?</code> just as before. In fact the most common standard use <code><a href="https://rdrr.io/r/base/mean.html">?mean</a></code> still works even when <em>{typed}</em> is attached.</p>
</div>
<div id="acknowledgements" class="section level2">
<h2 class="hasAnchor">
<a href="#acknowledgements" class="anchor"></a>Acknowledgements</h2>
<p>This is inspired in good part by Jim Hester and Gabor Csardi’s work and many great efforts on static typing, assertions, or annotations in R, in particular:</p>
<ul>
<li>Gabor Csardy’s <em>{argufy}</em>
</li>
<li>Richie Cotton’s <em>{assertive}</em>
</li>
<li>Tony Fishettti’s <em>{assertr</em>}</li>
<li>Hadley Wickham’s <em>{assertthat}</em>
</li>
<li>Michel Lang’s <em>{checkmate}</em>
</li>
<li>Joe Thorley’s <em>{checkr}</em>
</li>
<li>Joe Thorley’s <em>{chk}</em>
</li>
<li>Aviral Goel’s <em>{contractr}</em>
</li>
<li>Stefan Bache’s <em>{ensurer}</em>
</li>
<li>Brian Lee Yung Rowe’s <em>{lambda.r}</em>
</li>
<li>Kun Ren’s <em>{rtype}</em>
</li>
<li>Jim Hester’s <em>{types}</em>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
<div class="links">
<h2>Links</h2>
<ul class="list-unstyled">
<li>Download from CRAN at <br><a href="https://cloud.r-project.org/package=typed">https://cloud.r-project.org/package=typed</a>
</li>
<li>Browse source code at <br><a href="https://github.com/moodymudskipper/typed/">https://github.com/moodymudskipper/typed/</a>
</li>
<li>Report a bug at <br><a href="https://github.com/moodymudskipper/typed/issues">https://github.com/moodymudskipper/typed/issues</a>
</li>
</ul>
</div>
<div class="license">
<h2>License</h2>
<ul class="list-unstyled">
<li><a href="https://www.r-project.org/Licenses/GPL-3">GPL-3</a></li>
</ul>
</div>
<div class="developers">
<h2>Developers</h2>
<ul class="list-unstyled">
<li>Antoine Fabri <br><small class="roles"> Author, maintainer </small> </li>
</ul>
</div>
<div class="dev-status">
<h2>Dev status</h2>
<ul class="list-unstyled">
<li><a href="https://travis-ci.com/moodymudskipper/typed"><img src="https://travis-ci.com/moodymudskipper/typed.svg?branch=iteration2" alt="Travis build status"></a></li>
<li><a href="https://codecov.io/gh/moodymudskipper/typed?branch=master"><img src="https://codecov.io/gh/moodymudskipper/typed/branch/master/graph/badge.svg" alt="Codecov test coverage"></a></li>
</ul>
</div>
</div>
</div>
<footer><div class="copyright">
<p>Developed by Antoine Fabri.</p>
</div>
<div class="pkgdown">
<p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
</div>
</footer>
</div>
</body>
</html>