forked from elixir-lang/elixir-lang.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.Typespec.html
More file actions
405 lines (329 loc) · 12.6 KB
/
Copy pathKernel.Typespec.html
File metadata and controls
405 lines (329 loc) · 12.6 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Kernel.Typespec</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
relpath = '';
if (relpath != '') relpath += '/';
</script>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
</head>
<body>
<script type="text/javascript" charset="utf-8">
if (window.top.frames.main) document.body.className = 'frames';
</script>
<div id="content">
<h1>
Kernel.Typespec
</h1>
<div id="moduledoc" class="docstring">
<p>Holds macros and functions for working with typespecs.</p>
<p>The attributes <code>@type</code>, <code>@opaque</code>, <code>@typep</code>, <code>@spec</code> and
<code>@callback</code> available in modules are handled by the equivalent
macros defined by this module.</p>
<h2>Defining a type</h2>
<p>@type type<em>name :: type
@typep type</em>name :: type
@opaque type_name :: type</p>
<p>For more details, see documentation for deftype, deftypep and defopaque in
Kernel.Typespec</p>
<h2>Defining a specification</h2>
<p>@spec function<em>name(type, type) :: type
@callback function</em>name(type, type) :: type</p>
<p>For more details, see documentation for defspec and defcallback in
Kernel.Typespec</p>
<h2>Types</h2>
<p>The type syntax provided by Elixir is fairly similar to the one
in Erlang.</p>
<p>Most of the built-in types provided in Erlang (for example, <code>pid()</code>)
are expressed the same way: <code>pid()</code> or simply <code>pid</code>. Parametrized types
are also supported: <code>list(integer())</code> and so are remote types: <code>Enum.t</code>.</p>
<p>Certain data type shortcuts ([...], <<>> and {...}) are supported as well.</p>
<p>Main differences lie in how bit strings and functions are defined:</p>
<h3>Bit Strings</h3>
<p>Bit string with a base size of 3:</p>
<pre><code><<_ :: 3>>
</code></pre>
<p>Bit string with a unit size of 8:</p>
<pre><code><<_ :: _ * 8>>
</code></pre>
<h3>Functions</h3>
<p>Any function:</p>
<pre><code>(fun(...) -> any)
or
((...) -> any)
or
(... -> any)
</code></pre>
<p>Function with arity of zero:</p>
<pre><code>(fun() -> type)
or
(() -> type)
</code></pre>
<p>Function with some arity:</p>
<pre><code>(fun(type, type) -> type)
or
((type, type) -> type)
or
(type, type -> type)
</code></pre>
<h2>Notes</h2>
<p>Elixir discourages the use of type <code>string()</code> as it might be confused
with binaries which are referred to as "strings" in Elixir (as opposed to
character lists). In order to use the type that is called <code>string()</code> in Erlang,
one has to use the <code>char_list()</code> type which is a synonym to <code>string()</code>. If yu
use <code>string()</code>, you'll get a warning from the compiler.</p>
<p>If you want to refer to the "string" type (the one operated by functions in the
String module), use <code>String.t()</code> type instead.</p>
<p>See <a href="http://www.erlang.org/doc/reference_manual/typespec.html">http://www.erlang.org/doc/reference_manual/typespec.html</a>
for more information.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L1" target="_blank" class="view_source">Source</a>
<h2>Functions summary</h2>
<ul class="summary">
<li>
<span class="summary_signature">
<a href="#beam_callbacks/1">beam_callbacks/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#beam_specs/1">beam_specs/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#beam_types/1">beam_types/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#define_callback/3">define_callback/3</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#define_spec/3">define_spec/3</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#define_type/3">define_type/3</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#defines_callback?/3">defines_callback?/3</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#defines_spec?/3">defines_spec?/3</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#defines_type?/3">defines_type?/3</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#spec_to_ast/2">spec_to_ast/2</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#type_to_ast/1">type_to_ast/1</a>
</span>
</li>
</ul>
<h2>Macros summary</h2>
<ul class="summary">
<li>
<span class="summary_signature">
<a href="#defcallback/1">defcallback/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#defopaque/1">defopaque/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#defspec/1">defspec/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#deftype/1">deftype/1</a>
</span>
</li>
<li>
<span class="summary_signature">
<a href="#deftypep/1">deftypep/1</a>
</span>
</li>
</ul>
<div id="functions_details" class="details_list">
<h2>Functions</h2>
<div class="detail">
<p class="signature" id="beam_callbacks/1">
<strong>beam_callbacks(module)</strong>
</p>
<div class="docstring"><p>Returns all callbacks available from the beam.</p>
<p>It is returned as a list of tuples where the first
element is spec name and arity and the second is the spec.</p>
<p>The module has to have a corresponding beam file
on the file system.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L313" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="beam_specs/1">
<strong>beam_specs(module)</strong>
</p>
<div class="docstring"><p>Returns all specs available from the beam.</p>
<p>It is returned as a list of tuples where the first
element is spec name and arity and the second is the spec.</p>
<p>The module has to have a corresponding beam file
on the file system.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L300" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="beam_types/1">
<strong>beam_types(module)</strong>
</p>
<div class="docstring"><p>Returns all types available from the beam.</p>
<p>It is returned as a list of tuples where the first
element is the type (<code>:typep</code>, <code>:type</code> and <code>:opaque</code>).</p>
<p>The module has to have a corresponding beam file
on the file system.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L273" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="define_callback/3">
<strong>define_callback(module, tuple, definition)</strong>
</p>
<div class="docstring"><p>Defines a <code>callback</code> by receiving Erlang's typespec.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L192" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="define_spec/3">
<strong>define_spec(module, tuple, definition)</strong>
</p>
<div class="docstring"><p>Defines a <code>spec</code> by receiving Erlang's typespec.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L185" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="define_type/3">
<strong>define_type(module, kind, type)</strong>
</p>
<div class="docstring"><p>Defines a <code>type</code>, <code>typep</code> or <code>opaque</code> by receiving Erlang's typespec.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L168" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="defines_callback?/3">
<strong>defines_callback?(module, name, arity)</strong>
</p>
<div class="docstring"><p>Returns true if the current module defines a callback.
This function is only available for modules being compiled.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L220" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="defines_spec?/3">
<strong>defines_spec?(module, name, arity)</strong>
</p>
<div class="docstring"><p>Returns true if the current module defines a given spec.
This function is only available for modules being compiled.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L211" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="defines_type?/3">
<strong>defines_type?(module, name, arity)</strong>
</p>
<div class="docstring"><p>Returns true if the current module defines a given type
(private, opaque or not). This function is only available
for modules being compiled.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L201" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="spec_to_ast/2">
<strong>spec_to_ast(name, arg2)</strong>
</p>
<div class="docstring"><p>Converts a spec clause back to Elixir AST.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L228" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="type_to_ast/1">
<strong>type_to_ast(arg1)</strong>
</p>
<div class="docstring"><p>Converts a type clause back to Elixir AST.</p>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L252" target="_blank" class="view_source">Source</a>
</div>
</div>
<div id="macros_details" class="details_list">
<h2>Macros</h2>
<div class="detail">
<p class="signature" id="defcallback/1">
<strong>defcallback(spec)</strong>
</p>
<div class="docstring"><p>Defines a callback.
This macro is the one responsible to handle the attribute @callback.</p>
<h2>Examples</h2>
<pre><code>@callback add(number, number) :: number
</code></pre>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L157" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="defopaque/1">
<strong>defopaque(type)</strong>
</p>
<div class="docstring"><p>Defines an opaque type.
This macro is the one responsible to handle the attribute @opaque.</p>
<h2>Examples</h2>
<pre><code>@opaque my_type :: atom
</code></pre>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L112" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="defspec/1">
<strong>defspec(spec)</strong>
</p>
<div class="docstring"><p>Defines a spec.
This macro is the one responsible to handle the attribute @spec.</p>
<h2>Examples</h2>
<pre><code>@spec add(number, number) :: number
</code></pre>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L142" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="deftype/1">
<strong>deftype(type)</strong>
</p>
<div class="docstring"><p>Defines a type.
This macro is the one responsible to handle the attribute @type.</p>
<h2>Examples</h2>
<pre><code>@type my_type :: atom
</code></pre>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L97" target="_blank" class="view_source">Source</a>
</div><div class="detail">
<p class="signature" id="deftypep/1">
<strong>deftypep(type)</strong>
</p>
<div class="docstring"><p>Defines a private type.
This macro is the one responsible to handle the attribute @typep.</p>
<h2>Examples</h2>
<pre><code>@typep my_type :: atom
</code></pre>
</div>
<a href="https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/kernel/typespec.ex#L127" target="_blank" class="view_source">Source</a>
</div>
</div>
</div>
</body>
</html>